iPhone上图像的文件路径 [英] File path of image on iPhone

查看:172
本文介绍了iPhone上图像的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm interested in uploading a file (image) from an iPhone library/camera roll to a remote web server. I already have a script working that will upload any file from the phone to a web server. However, I assume that to upload an image from the iPhone, I need the PATH to said image. Is there any way this can be done, once the user picks said image from the camera roll? I.e., how do I get the file path of an image selected in the camera roll?

我尝试无济于事.

谢谢!

推荐答案

您将需要查看

You will want to look at the ALAssetsLibrary functions - these let you access photos and videos that are stored in your Photos and Videos libraries on your iOS device.

具体来说,是这样的:

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而不是纯粹的编程方法,则可以大大简化它:

If you are using the UIImagePickerController rather than a purely programatical approach, this simplifies it greatly:

在:

- (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];
}

这篇关于iPhone上图像的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆