Cropping center square of UIImage Cropping center square of UIImage ios ios

Cropping center square of UIImage


I think here would be the perfect solution!
It is NOT good idea to crop image basis on the toSize's size. It will look weird when the image resolution (size) is very large.
Following code will crop the image as per the toSize's ratio.
Improved from @BlackRider's answer.

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size{    double newCropWidth, newCropHeight;    //=== To crop more efficently =====//    if(image.size.width < image.size.height){         if (image.size.width < size.width) {                 newCropWidth = size.width;          }          else {                 newCropWidth = image.size.width;          }          newCropHeight = (newCropWidth * size.height)/size.width;    } else {          if (image.size.height < size.height) {                newCropHeight = size.height;          }          else {                newCropHeight = image.size.height;          }          newCropWidth = (newCropHeight * size.width)/size.height;    }    //==============================//    double x = image.size.width/2.0 - newCropWidth/2.0;    double y = image.size.height/2.0 - newCropHeight/2.0;    CGRect cropRect = CGRectMake(x, y, newCropWidth, newCropHeight);    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);    UIImage *cropped = [UIImage imageWithCGImage:imageRef];    CGImageRelease(imageRef);    return cropped;}


This works with different orientations. Both portrait and landscape orientations works correctly.

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size{    // not equivalent to image.size (which depends on the imageOrientation)!    double refWidth = CGImageGetWidth(image.CGImage);    double refHeight = CGImageGetHeight(image.CGImage);    double x = (refWidth - size.width) / 2.0;    double y = (refHeight - size.height) / 2.0;    CGRect cropRect = CGRectMake(x, y, size.height, size.width);    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);    UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:self.imageOrientation];    CGImageRelease(imageRef);    return cropped;}


Based on the checked answer by Elmundo and its swift version by Imbrue, here is the same solution that automatically calculates the size of the image's center (taking orientation into account), and with error consideration:

func cropImageToSquare(image: UIImage) -> UIImage? {    var imageHeight = image.size.height    var imageWidth = image.size.width    if imageHeight > imageWidth {        imageHeight = imageWidth    }    else {        imageWidth = imageHeight    }    let size = CGSize(width: imageWidth, height: imageHeight)    let refWidth : CGFloat = CGFloat(CGImageGetWidth(image.CGImage))    let refHeight : CGFloat = CGFloat(CGImageGetHeight(image.CGImage))    let x = (refWidth - size.width) / 2    let y = (refHeight - size.height) / 2    let cropRect = CGRectMake(x, y, size.height, size.width)    if let imageRef = CGImageCreateWithImageInRect(image.CGImage, cropRect) {        return UIImage(CGImage: imageRef, scale: 0, orientation: image.imageOrientation)    }   return nil}

Swift 3 version

func cropImageToSquare(image: UIImage) -> UIImage? {    var imageHeight = image.size.height    var imageWidth = image.size.width    if imageHeight > imageWidth {        imageHeight = imageWidth    }    else {        imageWidth = imageHeight    }    let size = CGSize(width: imageWidth, height: imageHeight)    let refWidth : CGFloat = CGFloat(image.cgImage!.width)    let refHeight : CGFloat = CGFloat(image.cgImage!.height)    let x = (refWidth - size.width) / 2    let y = (refHeight - size.height) / 2    let cropRect = CGRect(x: x, y: y, width: size.height, height: size.width)    if let imageRef = image.cgImage!.cropping(to: cropRect) {        return UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)    }    return nil}


matomo