Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Cropping image

Any ideas on how to do that? Any tutorial maybe

Love

1 Answer

Depends on the type of cropping you want to achieve. If you want users to be able to crop an image they've taken with the camera or picked from the gallery, you can set the allowsEditing property to YES on the UIImagePickerController, like this:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.allowsEditing = YES;

In that case, you'll also have to grab the edited image, not the original one in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info like this:

UIImage *image = info[UIImagePickerControllerEditedImage];

But if you want to crop an UIImage you already have somewhere in your code, then this tutorial/example should help.