Skip to content

Instantly share code, notes, and snippets.

@danmartyn
Created June 15, 2013 08:22
Show Gist options
  • Save danmartyn/5787383 to your computer and use it in GitHub Desktop.
Save danmartyn/5787383 to your computer and use it in GitHub Desktop.
UIImagePickerController Delegate Methods
#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