Created
June 15, 2013 08:22
-
-
Save danmartyn/5787383 to your computer and use it in GitHub Desktop.
UIImagePickerController Delegate Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - | |
#pragma mark UIImagePickerController Delegate Methods | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | |
{ | |
// dismiss the picker | |
if (self.popover) | |
{ | |
[self.popover dismissPopoverAnimated:YES]; | |
self.popover = nil; | |
} else | |
{ | |
[picker dismissViewControllerAnimated:YES completion:nil]; | |
} | |
// get the edited image from the picker | |
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; | |
// if we didn't get an edited image back, check for an original image | |
if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage]; | |
// if we still don't have an image, check for what was in the crop rectangle | |
if (!image) image = [info objectForKey:UIImagePickerControllerCropRect]; | |
// if the user took a picture, save it to their photo roll | |
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) | |
{ | |
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; | |
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ | |
if (error) | |
{ | |
NSLog(@"Error saving the picture: %@", error); | |
} | |
}]; | |
} | |
// resize the image before sending it back | |
image = [image resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:self.imagePickerView.frame.size interpolationQuality:kCGInterpolationHigh]; | |
// post a notification with the image | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"User Chose Image For Image View Element" object:self.imagePickerView userInfo:@{@"image":image}]; | |
} | |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker | |
{ | |
[picker dismissViewControllerAnimated:YES completion:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment