简体   繁体   中英

File path of image on iPhone

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? Ie, how do I get the file path of an image selected in the camera roll?

I have tried to no avail.

Thanks!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM