The UIImage Class provides a method that can save an image straight to the Camera Roll. In this example, yourImage is a UIImage:
1 2 |
// super simple saving UIImageWriteToSavedPhotosAlbum(yourImage, nil, nil, nil); |
That’s short and sweet – but there’s no feedback by default, like a delegate method that informs you of how this operation went.
A much better option is to make use of the selector this method can call upon success or failure, like so:
[emember_protected]
1 2 |
// saving and calling a selector UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); |
This will call the following method which will contain an NSError object. Here we test if it contains an error message, and display it if it does. If all went well a success message is displayed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { // called when the image was saved to the camera roll if (error.localizedDescription == NULL) { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"Your image was saved to the Camera Roll." delegate:self cancelButtonTitle:@"Thanks!" otherButtonTitles:nil, nil]; [alertView show]; } else { // display error message NSString *errorMessage = [NSString stringWithFormat:@"The image could not be saved today. %@.", error.localizedDescription]; UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Houston, we have a problem!" message:errorMessage delegate:self cancelButtonTitle:@"I'll try later" otherButtonTitles:nil, nil]; [errorView show]; } } |
[/emember_protected]