【问题标题】:File path of image on iPhoneiPhone上图像的文件路径
【发布时间】:2012-07-19 21:59:52
【问题描述】:

我有兴趣将文件(图像)从 iPhone 库/相机胶卷上传到远程 Web 服务器。我已经有一个脚本可以将任何文件从手机上传到网络服务器。但是,我假设要从 iPhone 上传图像,我需要该图像的路径。一旦用户从相机胶卷中选择所述图像,有什么方法可以做到这一点?即,如何获取相机胶卷中所选图像的文件路径?

我试过没有用。

谢谢!

【问题讨论】:

    标签: iphone objective-c ios photo


    【解决方案1】:

    您将需要查看ALAssetsLibrary 函数 - 这些函数可让您访问存储在 iOS 设备上的照片和视频库中的照片和视频。

    具体来说,类似:

    ALAssetsLibrary *assets = [[ALAssetsLibrary alloc] init];
    
    [assets enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
        usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
                //the ALAsset should be one of your photos - stick it in an array and after this runs, use it however you need
            }
        }
        failureBlock:^(NSError *error) {
            //something went wrong, you can't access the photo gallery
        }
    ];
    

    编辑

    如果您使用的是 UIImagePickerController 而不是纯粹的编程方法,这将大大简化它:

    在:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
        //you can use UIImagePickerControllerOriginalImage for the original image
    
        //Now, save the image to your apps temp folder, 
    
        NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"upload-image.tmp"];
        NSData *imageData = UIImagePNGRepresentation(img);
        //you can also use UIImageJPEGRepresentation(img,1); for jpegs
        [imageData writeToFile:path atomically:YES];
    
        //now call your method
        [someClass uploadMyImageToTheWebFromPath:path];
    }
    

    【讨论】:

    • 没有那个库有没有办法做到这一点?我需要访问任何给定的照片 X,只是他们从选择器中单击的那张。
    • 出于某种原因,我认为您试图纯粹以编程方式进行。如果您使用的是 UIImagePickerController,那么它变得非常非常容易。我将编辑我的答案以反映这一点。
    • @CrimsonDiego 我使用了你的代码。但是如何检查特定照片并获取其路径。
    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    相关资源
    最近更新 更多