File path of image on iPhone

和自甴很熟 提交于 2020-01-07 08:32:02

问题


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?

I have tried to no avail.

Thanks!


回答1:


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.

Specifically, something like:

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

EDIT

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

In:

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


来源:https://stackoverflow.com/questions/11569968/file-path-of-image-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!