Database Reference
In-Depth Information
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
.
Image Picker Delegate Methods
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];
NSData *imageData = UIImagePNGRepresentation(resizedImage);
[_closeFriend setImage:imageData];
[_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 × 560 using the method
resizeImage:interpolation
Quality
: that is provided in the UIImage+Resize Category on
UIImage
. Next we create an
NSData
object
using the
UIImagePNGRepresentation()
method and pass it the resized image. We take that
NSData
and
assign it to the image property of
_closeFriend
. Then we assign the image to the
_imgFriend
image
view and dismiss our image picker.
Search WWH ::
Custom Search