UIImagePickerController - 从应用程序文档目录保存和检索照片
Posted
技术标签:
【中文标题】UIImagePickerController - 从应用程序文档目录保存和检索照片【英文标题】:UIImagePickerController - Save and Retrieve photo from Apps Document Directory 【发布时间】:2012-03-23 03:05:00 【问题描述】:这快把我逼疯了!
如果我从相机胶卷中选择现有照片,我已成功将照片保存到我的应用程序文档目录中。执行此操作的代码如下。注意我知道这部分工作正常,因为在模拟器中我可以浏览到应用程序的 Documents 文件夹并查看正在保存的文件。
“保存”代码示例:
//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
一旦图像被拍摄或选择:
- (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;
所以,我得到一个空按钮,单元格中没有显示任何图像。我还用按钮替换了 UIImageView,但仍然没有运气......
【问题讨论】:
您好,刚刚阅读您的问题 - 是 util_,我希望您不介意我问,但是 util_ 声明为什么? util_ 只是我创建的一个实用程序类,里面有一堆实用程序类型的方法,没什么特别的。 【参考方案1】:你的问题在这里:
UIImage *image = [UIImage imageNamed:fullPathToFile];
UIImage imageNamed:
仅适用于捆绑包中的图像。它不适用于文档目录中的图像。
改用imageWithContentsOfFile:
。
【讨论】:
啊!那么我应该保存在其他地方还是将其保存在文档中并使用其他方法来获取它?那另一种方法是什么?以上是关于UIImagePickerController - 从应用程序文档目录保存和检索照片的主要内容,如果未能解决你的问题,请参考以下文章
UIImagePickerController、自定义 UIButton 和 AutoLayout
OCMock 模拟 UIImagePickerController