Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. Camera and the Photo Library > 10.4. Storing Photos in the Photo Library

10.4. Storing Photos in the Photo Library

Problem

You want to be able to store a photo in the user’s photo library.

Solution

Use the UIImageWriteToSavedPhotosAlbum procedure:

- (void) imageWasSavedSuccessfully:(UIImage *)paramImage
          didFinishSavingWithError:(NSError *)paramError
                       contextInfo:(void *)paramContextInfo{

  if (paramError == nil){
    NSLog(@"Image was saved successfully.");
  } else {
    NSLog(@"An error happened while saving the image.");
    NSLog(@"Error = %@", paramError);
  }

}

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

  NSLog(@"Picker returned successfully.");

  NSString     *mediaType = [info objectForKey:
                             UIImagePickerControllerMediaType];

  if ([mediaType isEqualToString:(NSString *)kUTTypeImage] == YES){

    UIImage *theImage = nil;

    if (picker.allowsEditing == YES){

      theImage =
      [info
       objectForKey:UIImagePickerControllerEditedImage];

    } else {

      theImage =
      [info
       objectForKey:UIImagePickerControllerOriginalImage];

    }

    /* To avoid a very long line of text, we will break the
     selector's name into two lines using the backslash */
    NSString *targetSelectorAsString =
    @"imageWasSavedSuccessfully:didFinishSavingWithError:\
    contextInfo:";

    SEL targetSelector =
    NSSelectorFromString(targetSelectorAsString);

    NSLog(@"Saving the photo...");
    UIImageWriteToSavedPhotosAlbum(theImage,
                                   self,
                                   targetSelector,
                                   nil);

  }

  [picker dismissModalViewControllerAnimated:YES];


}

- (void) imagePickerControllerDidCancel
         :(UIImagePickerController *)picker{

  NSLog(@"Picker was cancelled");

  [picker dismissModalViewControllerAnimated:YES];

}

- (void)viewDidLoad {
  [super viewDidLoad];

  if ([self isCameraAvailable] == YES &&
      [self doesCameraSupportTakingPhotos] == YES){

    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];

    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;

    imagePicker.mediaTypes =
    [UIImagePickerController
     availableMediaTypesForSourceType:
     imagePicker.sourceType];

    imagePicker.allowsEditing = YES;

    imagePicker.delegate = self;

    [self presentModalViewController:imagePicker
                            animated:YES];

    [imagePicker release];

  } else {
    NSLog(@"The camera is not available.");
  }

}

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint