Create a square UIImage crop that stretches the image

When I try to perform central cropping of UIImage, I get the following results (on the left is the original image in 640*1136 , and on the right is the cropped image set to the UIImageView square in 320*320 ):

enter image description here refers to: enter image description here

I worked a bit with the relationship element so that it can correctly determine the cropping value: using the shorter side of the image, building the ratio based on short_side/width_of_desired_rect , but it doesn't seem to work in this case. Help rate!

 - (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { double ratio; double delta; CGPoint offset; //make a new square size, that is the resized imaged width CGSize sz = CGSizeMake(newSize.width, newSize.width); //figure out if the picture is landscape or portrait, then //calculate scale factor and offset if (image.size.width > image.size.height) { ratio = newSize.width / image.size.width; delta = (ratio*image.size.width - ratio*image.size.height); offset = CGPointMake(delta/2, 0); } else { ratio = newSize.width / image.size.height; delta = (ratio*image.size.height - ratio*image.size.width); offset = CGPointMake(0, delta/2); } //make the final clipping rect based on the calculated values CGRect clipRect = CGRectMake(-offset.x, -offset.y, (ratio * image.size.width) + delta, (ratio * image.size.height) + delta); //for retina consideration if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0); } else { UIGraphicsBeginImageContext(sz); } UIRectClip(clipRect); [image drawInRect:clipRect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 
+7
ios objective-c uiimage
source share
3 answers

Try the following:

 - (UIImage *)squareImageFromImage:(UIImage *)image scaledToSize:(CGFloat)newSize { CGAffineTransform scaleTransform; CGPoint origin; if (image.size.width > image.size.height) { CGFloat scaleRatio = newSize / image.size.height; scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio); origin = CGPointMake(-(image.size.width - image.size.height) / 2.0f, 0); } else { CGFloat scaleRatio = newSize / image.size.width; scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio); origin = CGPointMake(0, -(image.size.height - image.size.width) / 2.0f); } CGSize size = CGSizeMake(newSize, newSize); if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { UIGraphicsBeginImageContextWithOptions(size, YES, 0); } else { UIGraphicsBeginImageContext(size); } CGContextRef context = UIGraphicsGetCurrentContext(); CGContextConcatCTM(context, scaleTransform); [image drawAtPoint:origin]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 
+41
source share

Swift3 version of fumoboy007 code ..

  func squareImage(img: UIImage, scaledToSize newSize: CGFloat) -> UIImage { var scaleTransform: CGAffineTransform var origin: CGPoint var image = img if image.size.width > image.size.height { let scaleRatio: CGFloat = newSize / image.size.height scaleTransform = CGAffineTransform(scaleX: scaleRatio, y: scaleRatio) origin = CGPoint(x: -(image.size.width - image.size.height) / 2.0, y: 0) } else { let scaleRatio: CGFloat = newSize / image.size.width scaleTransform = CGAffineTransform(scaleX: scaleRatio, y: scaleRatio) origin = CGPoint(x: 0, y: -(image.size.height - image.size.width) / 2.0) } let size = CGSize(width: newSize, height: newSize) if image.size.width > image.size.height { UIGraphicsBeginImageContextWithOptions(size, true, 0) } else { UIGraphicsBeginImageContext(size) } let context: CGContext? = UIGraphicsGetCurrentContext() context?.concatenate(scaleTransform) image.draw(at: origin) image = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return image } 
+5
source share

Use this code to resize the image.

 // Returns image resized to the desired CGSize - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

Edit: here you are redrawing as well. If you reuse the same image over and over, it might be a good idea to call it once and save the result as a file. Download instead of the original instead in subsequent calls.

Edit 2: Good, so what you want to do is keep the aspect ratio when fitting the image into a square. The above code basically matches the image per square, resizing it, but it does not support aspect ratio. A way to maintain aspect ratio will be to find the coefficient by which you reduce the larger of the two image sizes, and then reduce the other dimension by the same coefficient. Let me know if you have a problem with this.

+2
source share

All Articles