Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to make a square image from a rectangle image (no matter horizontal or vertical) by cropping it by width or height in both sides, use my code. The difference is that I don't stretch, but I crop. I made it from the top code by modification: </p> <pre><code>//Cropping _image to fit it to the square by height or width CGFloat a = _image.size.height; CGFloat b = _image.size.width; CGRect cropRect; if (!(a==b)) { if (a&lt;b) { cropRect = CGRectMake((b-a)/2.0, 0, a, a); b = a; } else if (b&lt;a) { cropRect = CGRectMake(0, (a-b)/2.0, b, b); a = b; } CGImageRef imageRef = CGImageCreateWithImageInRect([_image CGImage], cropRect); UIImage* sourceImage = _image; CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); CGContextRef bitmap; if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); } else { bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); } if (sourceImage.imageOrientation == UIImageOrientationLeft) { CGContextRotateCTM (bitmap, radians(90)); CGContextTranslateCTM (bitmap, 0, -a); } else if (sourceImage.imageOrientation == UIImageOrientationRight) { CGContextRotateCTM (bitmap, radians(-90)); CGContextTranslateCTM (bitmap, -a, 0); } else if (sourceImage.imageOrientation == UIImageOrientationUp) { // NOTHING } else if (sourceImage.imageOrientation == UIImageOrientationDown) { CGContextTranslateCTM (bitmap, a, a); CGContextRotateCTM (bitmap, radians(-180.)); } CGContextDrawImage(bitmap, CGRectMake(0, 0, a, a), imageRef); CGImageRef ref = CGBitmapContextCreateImage(bitmap); _image = [UIImage imageWithCGImage:ref]; CGImageRelease(imageRef); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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