UIImagePickerController – 保存和检索从应用程序文档目录的照片
这真让我抓狂!
我成功地将照片保存到我的应用程序文档目录从这两个相机,如果我从相机胶卷select一个现有的。 执行此操作的代码如下所示。 注意我知道这部分工作,因为在模拟器中,我可以浏览到应用程序的文档文件夹,看到正在保存的文件。
示例“保存”代码:
//Note: code above snipped to keep this part of the question short case 1: { // select from library NSLog(@"select from camera roll"); if([util_ isPhotoLibraryAvailable]) { UIImagePickerController *controller = [[UIImagePickerController alloc] init]; controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; NSMutableArray *mediaTypes = [[NSMutableArray alloc] init]; if([util_ canUserPickPhotosFromPhotoLibrary]) { [mediaTypes addObject:(__bridge NSString *)kUTTypeImage]; } controller.mediaTypes = mediaTypes; controller.delegate = self; controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:controller animated:YES]; } } break; //code below snipped out
一旦图像被拍摄或select:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"picker returned successfully"); NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; if([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage]) { UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; UIImage *resizedImage = [util_ createThumbnailForImage:originalImage thumbnailSize:[util_ determineIPhoneScreenSize]]; NSData *imageData = UIImagePNGRepresentation(resizedImage); NSString* imageName = @"MyImage.png"; NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; [imageData writeToFile:fullPathToFile atomically:NO]; NSLog(@"fullPathToFile %@", fullPathToFile); // this outputs the following path in the debugger // fullPathToFile /Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/47B01A4C-C54F-45C4-91A3-C4D7FF9F95CA/Documents/MyImage.png // rest snipped out - at this point I see the image in the simulators/app/Documents directory
现在 – 不工作的部分(获取并显示照片):
// code above snipped out (we're in tableView:cellForRowAtIndexPath) imageButton_ = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 200, 200)]; NSString* imageName = [contentArray_ objectAtIndex:indexPath.row]; NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); SString* documentsDirectory = [paths objectAtIndex:0]; NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; UIImage *image = [UIImage imageNamed:fullPathToFile]; [imageButton_ setImage:image forState:UIControlStateNormal]; [cell addSubview:imageButton_]; NSLog(@"fullPathToFile: %@", fullPathToFile); // this outputs the following path in the debugger // fullPathToFile: /Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/47B01A4C-C54F-45C4-91A3-C4D7FF9F95CA/Documents/MyImage.png return cell;
}
所以,我得到一个没有图像显示在单元格中的空button。 我也replace了UIImageView的button,仍然没有运气…
你的问题在这里:
UIImage *image = [UIImage imageNamed:fullPathToFile];
UIImage imageNamed:
仅适用于捆绑中的图像。 它不适用于文档目录中的图像。
试试imageWithContentsOfFile:
取而代之。
- iOS 11:它如何影响您的移动应用测试
- 你如何定义CFBundleIcons,CFBundleIconFiles,CFBundleIconFile?
- UITapGestureRecognizer无法在iOS9上运行
- 将parsing的数据馈送到RKMapperOperation将引发NSUnknownKeyException
- 保证在正确的时间ios交付的UILocalNotification
- 从iOS设备validation到Google App Engine
- 在UITableViewCell中的UIImageView或AVPlayer(照片或video)
- reloadRowsAtIndexPaths自定义UITableViewCell外观不会更改后
- 如何为UIButton添加操作