2009-08-12 61 views
2

UIImagePickerView控制器返回图像的NSDataUIImagePickerView控制器 - 图像路径 - iphone

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

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

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

在此先感谢。

回答

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]; 
} 
+0

这意味着 - 图像被复制 - 图像存储在两个地方,在我的应用程序和在iPhone中,是这样吗? – 2009-08-12 22:59:37

+1

是的。不幸的是,苹果公司没有提供一种获取原始图像的方法。您可以通过手动查看用户的照片库(它位于/ private中)并尝试查找匹配(昂贵)的图像来非正式地进行此操作。无论如何,如果您将图像作为NSData存储在磁盘上,即使是巨大的图像(几百万像素)也不应占用太多空间。所以在现实中,这很少是一个问题。 – Itay 2009-08-12 23:16:20

+0

你能帮我吗,如何将UIImage存储到我的应用程序? – 2009-08-12 23:16:57