【问题标题】:UIImagePickerView Controller - image path - iphoneUIImagePickerView 控制器 - 图像路径 - iphone
【发布时间】:2009-08-12 22:39:39
【问题描述】:

UIImagePickerView控制器返回图片的NSData

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

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

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

提前致谢。

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    这是不可能的。 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];
    }
    

    【讨论】:

    • 这意味着 - 图像被复制 - 图像存储在两个地方,在我的应用程序和 iPhone 中,是这样吗?
    • 是的。不幸的是,Apple 没有提供获取原始图像的方法。您可以通过手动查看用户的照片库(位于 /private 中的某个位置)并尝试找到匹配的图像(成本高昂)来非正式地做到这一点。无论如何,如果您将图像作为 NSData 存储在磁盘上,即使是巨大的图像(几百万像素)也不应该占用太多空间。所以实际上,这很少成为问题。
    • 你能帮我吗,如何将该 UIImage 存储到我的应用程序中?
    猜你喜欢
    • 2010-11-29
    • 1970-01-01
    • 2014-03-21
    • 2015-12-04
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2012-03-05
    相关资源
    最近更新 更多