Database Reference
In-Depth Information
case 1: {
_picker = [[UIImagePickerController alloc] init];
[_picker setDelegate:self];
[_picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[_picker setAllowsEditing:YES];
[self presentViewController:_picker animated:YES completion:nil];
} break;
default:
break;
}
}
The method starts off by checking to see whether _picker has been allocated. If it has, then we call
the method
dismissViewControllerAnimated:completion
: to make sure the picker is not visible.
Then we assign nil to it to clear it out completely.
We use a switch statement to determine what button the user pressed. If they pressed Take
Photo, then that button's index is going to be 0 and if they pressed Choose From Library, it
will be 1. Inside of each case we instantiate the
UIImagePickerController
, set its delegate
to this view controller, set the source type to
UIImagePickerControllerSourceTypeCamera
or
UIImagePickerControllerSourceTypePhotoLibrary
depending on what button they pressed, and
set allows editing to YES. We then call the method
presentViewController:animated:completion
: on
our self to display the
UIImagePickerController
.
Now we need to add two
UIImagePickerControllerDelegate
methods.
#pragma mark - UIImagePickerControllerDelegate Methods
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = (UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
UIImage *resizedImage = [image resizedImage:CGSizeMake(560, 560)
interpolationQuality:kCGInterpolationHigh];
[_imgFriend setImage:resizedImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
The first method,
imagePickerControllerDidCancel
: is called when the user cancels the image picker.
The only thing we do here is dismiss the image picker, which brings the user back to our view.
The second method
imagePickerController: didFinishPickingMediaWithInfo
: is where we handle
the image the user either took or selected. Because we enabled editing, the user will be creating a
square image that we can access by using the constant
UIImagePickerControllerEditedImage
. We
take that image and then resize it to 560 ×650 using the method
resizeImage:interpolationQuality
:
that is provided in the
UIImage+Resize
Category on
UIImage
. Then we assign the image to the
_imgFriend
image view and dismiss our image picker.
Search WWH ::
Custom Search