您的位置:首页 > 产品设计 > UI/UE

UIImagePickerController Save to Disk then Load to UIImageView

2011-06-06 22:27 525 查看
地址:http://stackoverflow.com/questions/2434324/uiimagepickercontroller-save-to-disk-then-load-to-uiimageview

up vote0down votefavorite

Hi,

I have a UIImagePickerController that saves the image to disk as a png. When I try to load the PNG and set a UIImageView's
imageView.image
to the file, it is not displaying.

Here is my code:

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; NSData *imageData = UIImagePNGRepresentation(image);

// Create a file name for the image
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *imageName = [NSString stringWithFormat:@"photo-%@.png",
[dateFormatter stringFromDate:[NSDate date]]];
[dateFormatter release];

// Find the path to the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// Now we get the full path to the file
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

// Write out the data.
[imageData writeToFile:fullPathToFile atomically:NO];

// Set the managedObject's imageLocation attribute and save the managed object context
[self.managedObject setValue:fullPathToFile forKey:@"imageLocation"];
NSError *error = nil;
[[self.managedObject managedObjectContext] save:&error];

[self dismissModalViewControllerAnimated:YES];

}

Then here is how I try to load it:

self.imageView.backgroundColor = [UIColor lightGrayColor];
self.imageView.frame = CGRectMake(10, 10, 72, 72);
if ([self.managedObject valueForKey:@"imageLocation"] != nil) {
NSLog(@"Trying to load the imageView with: %@", [self.managedObject valueForKey:@"imageLocation"]);
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
self.imageView.image = image;

} else {
self.imageView.image = [UIImage imageNamed:@"no_picture_taken.png"];
}

I get the message that it's trying to load the imageView in the debugger, but the image is never displayed in the imageView. Can anyone tell me what I've got wrong here?

Thanks a bunch.

iphone uiimageview uiimagepickercontroller
link|edit|flag
asked Mar 12 '10 at 16:54



Harley Gagrow
11

try not saving the full path, just the image name and look up the path before displaying. – Run Loop Mar 13 '10 at 3:45

1 Answer

activeoldestvotes

up vote1down vote
You're writing out an NSData object, not an image. You need to read the NSData object back in and convert to UIImage.

Assuming everything else is correct, try this:

NSData *data = [[NSData alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
UIImage *image = [[UIImage alloc] initWithData:data];


link|edit|flag
answered Mar 13 '10 at 11:57



Jordan
5,69711326

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: