【发布时间】:2009-08-12 22:39:39
【问题描述】:
UIImagePickerView控制器返回图片的NSData,
我的要求是将图像的路径存储为 varchar 数据类型。
从UIImagePickerView中选择图片后,如何获取iPhone相册中所选图片的完整路径?
我的应用程序不必担心在我的应用程序中存储图像。这些图像已经存储在 iPhone 的照片库中。
提前致谢。
【问题讨论】:
标签: iphone cocoa-touch
UIImagePickerView控制器返回图片的NSData,
我的要求是将图像的路径存储为 varchar 数据类型。
从UIImagePickerView中选择图片后,如何获取iPhone相册中所选图片的完整路径?
我的应用程序不必担心在我的应用程序中存储图像。这些图像已经存储在 iPhone 的照片库中。
提前致谢。
【问题讨论】:
标签: iphone cocoa-touch
这是不可能的。 UIImagePickerController 会给你一个UIImage*,你必须自己把它转换成NSData,并存储在你的本地沙箱中。没有 SDK 批准的方式来访问用户照片库中的图像(出于安全原因)。
假设您使用的是 SDK 3.0,这里有一个选择器返回的函数,它将把它保存到磁盘,在您的应用程序的文档目录中(这应该可以工作,尽管我可能在某处有一个小错误):
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Dismiss the picker
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// Get the image from the result
UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
// Get the data for the image as a PNG
NSData* imageData = UIImagePNGRepresentation(image);
// Give a name to the file
NSString* imageName = @"MyImage.png";
// Now, we have to find the documents directory so we can save it
// Note that you might want to save it elsewhere, like the cache directory,
// or something similar.
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];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
}
【讨论】: