在iPhone上的图像文件路径

时间:2012-07-19 21:59:52

标签: iphone objective-c ios photo

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

我试着无济于事。

谢谢!

1 个答案:

答案 0 :(得分:6)

您需要查看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];
}