UIImagePickerView控制器 - 图像路径 - iphone

Sag*_*ari 2 iphone cocoa-touch

UIImagePickerView控制器返回NSData图像,

我的要求是将图像的路径存储为varchar数据类型.

从中选择图像后UIImagePickerView,如何获取iPhone照片库所选图像的完整路径?

我的应用程序不必担心在我的应用程序中存储图像.图像已存储在iPhone的照片库中.

提前致谢.

Ita*_*tay 7

这是不可能的.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];
}
Run Code Online (Sandbox Code Playgroud)