Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, you need to take the photo with the camera, so add this code where you want to take the picture.</p> <pre><code>UIImagePickerController *pickerController = [[UIImagePickerController alloc] init]; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; pickerController.delegate = self; [self presentViewController:pickerController animated:YES completion:nil]; } </code></pre> <p>Then, you need to implement delegate methods. Your class must conform to <code>UIImagePickerControllerDelegate</code>, documented <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/">here</a>. In the <code>imagePickerController: didFinishPickingMediaWithInfo:</code> method, you can take the taken photo in a <code>UIImage</code> with:</p> <pre><code>UIImage *imageTaked = info[UIImagePickerControllerOriginalImage]; </code></pre> <p>If you want, at this point you can take the metadata of the taken photo with</p> <pre><code>info [UIImagePickerControllerMediaMetadata]; </code></pre> <p>Finally, when you have your photo in a UIImage, you can crop the image as follows:</p> <pre><code>CGFloat x, y, side; //We will see how to calculate those values later. UIImage* imageCropped; CGRect cropRect = CGRectMake(x,y,side,side); CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaked CGImage], cropRect); imageCropped = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); </code></pre> <p>So, the important thing here is how to calculate the <code>cropRect</code> to cut the centre square of the UIImage. To do that, you can calculate <code>x</code>, <code>y</code> and <code>side</code> as follows:</p> <pre><code>side = MIN(imageTaked.size.width, imageTaked.size.height x = imageTaked.size.width / 2 - side / 2; y = imageTaked.size.height / 2 - side / 2; </code></pre> <p>Ok, so, to summarise, all the code inside the <code>imagePickerController: didFinishPickingMediaWithInfo:</code> method results in:</p> <pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *imageTaked = info[UIImagePickerControllerOriginalImage]; //You can take the metadata here =&gt; info [UIImagePickerControllerMediaMetadata]; UIImage* imageCropped; CGFloat side = MIN(imageTaked.size.width, imageTaked.size.height); CGFloat x = imageTaked.size.width / 2 - side / 2; CGFloat y = imageTaked.size.height / 2 - side / 2; CGRect cropRect = CGRectMake(x,y,side,side); CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaked CGImage], cropRect); imageCropped = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); } </code></pre> <p><strong>EDIT</strong></p> <p>If you want to send your photo to another device, probably the photo will be rotated. That's because Apple uses exif metadata for images, but other platforms don't. To solve this problem, just use this method to set appropriate metadata info:</p> <pre><code>-(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]; } </code></pre> <p>Hope it helps! and let me know if something goes wrong during copy-paste process, it's 2:00AM here, and i'm a zombie man right now!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload