First you need to take a picture with the camera, so add this code where you want to take a picture.
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init]; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; pickerController.delegate = self; [self presentViewController:pickerController animated:YES completion:nil]; }
Then you need to implement delegation methods. Your class must match the UIImagePickerControllerDelegate documented here . In the imagePickerController: didFinishPickingMediaWithInfo: you can take a taken photo in UIImage with:
UIImage *imageTaked = info[UIImagePickerControllerOriginalImage];
If you want, at this point you can take the metadata of the taken photo using
info [UIImagePickerControllerMediaMetadata];
Finally, when you have a photo in UIImage, you can crop the image as follows:
CGFloat x, y, side;
So, here it is important how to calculate cropRect in order to cut the center square of the UIImage. To do this, you can calculate x , y and side as follows:
side = MIN(imageTaked.size.width, imageTaked.size.height x = imageTaked.size.width / 2 - side / 2; y = imageTaked.size.height / 2 - side / 2;
So, to summarize, all the code inside the imagePickerController: didFinishPickingMediaWithInfo: method results in:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *imageTaked = info[UIImagePickerControllerOriginalImage];
EDIT
If you want to send your photo to another device, the photo may be rotated. This is because Apple uses exif metadata for images, but this is not the case on other platforms. To solve this problem, simply use this method to set the appropriate metadata information:
-(UIImage*)getImageWithCorrectFiltersFromOriginalImage:(UIImage *)originalImage { UIImageOrientation orientation = UIImageOrientationUp; NSNumber* orientationValue = [originalImage valueForProperty:@"ALAssetPropertyOrientation"]; if (orientationValue) { orientation = [orientationValue intValue]; } ALAssetRepresentation *assetRep = [originalImage defaultRepresentation]; CGImageRef imageRef = [assetRep fullResolutionImage]; return [UIImage imageWithCGImage:imageRef scale:[assetRep scale] orientation:orientation]; }
Hope this helps! and let me know if something goes wrong during the copy-paste process, it's 2:00 AM here, and I'm a zombie man now!