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.imageto 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
| ||
|
1 Answer
activeoldestvotesup 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];
|
相关文章推荐
- iPhone - UIImagePickerController -> save the image to app folder
- How to select Multiple images from UIImagePickerController [duplicate]
- assigning to uiimagepickercontrollerdelegate from incompatible type
- ios UIImagePickerController 添加一个自定义的view。
- uiimagePickerViewController
- 用UIImagePickerViewController自定义相机界面
- iOS - 解决Warning: Attempt to present <UIImagePickerController: 0x7f9ba106a000>which is already presen
- 自定义照相机界面之cameraOverlayView和UIImagePickerControllerEditedImage
- UIImagePickerController as a sub view instead of Modal View
- UIImagePickerViewController裁切头像
- ios UIImagePickerController 添加一个自定义的view
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
- 获取手机照片UIImagePickerViewController
- 关于UIImagePickerViewController拍摄video的分辨率和方向的一些代码整理
- Attempt to present <UIImagePickerController: 0x1848de00> on <UINavigationController: 0x17e6ed50> wh
- UIAlertController:Attempting to load the view of a view controller while it is deallocating is not a
- 本地化MFMessageComposeViewController,UIImagePickerController等系统页面
- 用UIImagePickerViewController的OverlayView自定义相机界面
- uiimagepickerviewcontroller
- 用UIImagePickerViewController自定义相机界面