0

I am taking a photo in xcode using a custom camera. Instead of the image being the normal size, I would like to crop it to a square. I have some code that I think should work but instead of cropping, it is squishing the image. What is going wrong? Thanks in advance!

Here is the code I am using to crop the image:

self.thumbImage = [self.photoImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit
                                                                  bounds:CGSizeMake(320.0f, 320.0f)
                                                    interpolationQuality:kCGInterpolationHigh];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
matthew
  • 467
  • 8
  • 23
  • I'm not familiar with this method (must be from some category of your own). I use the following category, which when I call `scaleImageToSizeAspectFill` or `scaleImageToSizeAspectFit`, doesn't result in any distortion: http://stackoverflow.com/a/10491692/1271826 – Rob Dec 07 '13 at 01:45
  • Are you want to save this image cropped or you will just display it in a UIImageView? – Hani Ibrahim Dec 07 '13 at 03:45
  • save the imaged cropped @HaniIbrahim – matthew Dec 07 '13 at 05:17

2 Answers2

0

Check this link https://stackoverflow.com/a/712553/1722480

All you need is to get your cropRectand then use CGImageCreateWithImageInRect method

UIImage *image = [UIImage imageNamed:@"4.png"];
CGRect cropRect;
if (image.size.width > image.size.height) {
    // Landscape Image
    cropRect = CGRectMake((image.size.width-image.size.height)/2.0, 0, image.size.height, image.size.height);
} else {
    // Portrait Image
    cropRect = CGRectMake(0, (image.size.height-image.size.width)/2.0, image.size.width, image.size.width);
}

// Crop Image
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

// Save Image
NSData *imageData = UIImagePNGRepresentation(croppedImage);
NSString *documentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[imageData writeToFile:[documentFolder stringByAppendingPathComponent:@"croppedImage.png"] atomically:YES];
Community
  • 1
  • 1
Hani Ibrahim
  • 1,448
  • 11
  • 21
  • Thanks, this worked great. My only question is if there is any way to make the quality of the image better. Now the shape is right but the quality is really bad – matthew Dec 08 '13 at 19:03
0

If you're simply wanting to display the photo on screen cropped to a square,add this code,

imageView.layer.maskToBounds = YES;

Where imageView is the name you have given your imageView object, and you've set it as a square. You will need to also add this to your header file

#import <QuartzCore/QuartzCore.h>

I hope this helps,

Cheers Jim.

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48
  • no need to use the layer. [UIView's `clipsToBounds`](https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW60) should give you the same results. – vikingosegundo Dec 07 '13 at 06:37