Storing a reference to an image saved in the Camera Roll

前端 未结 1 900
不思量自难忘°
不思量自难忘° 2021-01-03 17:09

I\'ve been working on a simple application which allows the user to take a photo and store it in the camera roll for later use. To do this, I am using a UIImagePickerControl

相关标签:
1条回答
  • 2021-01-03 17:38

    It turns out that it is actually not that hard to do this. In the UIImagePickerDelegate method:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
    

    the NSDictionary "info" actually contains the url to the image or the movie which is stored on the Camera Roll. You need only check:

    if([[info valueForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]){ // If the user took a video,
      movieURL = [[info valueForKey:UIImagePickerControllerMediaURL] retain];// Get the URL of where the movie is stored.
      UISaveVideoAtPathToSavedPhotosAlbum([movieURL path], nil, nil, nil);   // Save the movie to the photo album.
    }
    

    and this will save the movie to the Camera Roll, as well as record the url. Doing it for a photo is analogous, just replace the "kUTTypeMovie" with "kUTTypeImage", and replace

    movieURL = [[info valueForKey:UIImagePickerControllerMediaURL] retain];// Get the URL of where the movie is stored.
    UISaveVideoAtPathToSavedPhotosAlbum([movieURL path], nil, nil, nil);   // Save the movie to the photo album.
    

    with

    UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage];    
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    

    If you need to store the UIImage not on the Camera Roll, there is a great post on stackoverflow Saving UIImage with NSCoding that you can use to store the image in your app. Hope that helps someone!

    0 讨论(0)
提交回复
热议问题