UIImage: Resize, then Crop UIImage: Resize, then Crop ios ios

UIImage: Resize, then Crop


I needed the same thing - in my case, to pick the dimension that fits once scaled, and then crop each end to fit the rest to the width. (I'm working in landscape, so might not have noticed any deficiencies in portrait mode.) Here's my code - it's part of a categeory on UIImage. Target size in my code is always set to the full screen size of the device.

@implementation UIImage (Extras)#pragma mark -#pragma mark Scale and crop image- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize{    UIImage *sourceImage = self;    UIImage *newImage = nil;        CGSize imageSize = sourceImage.size;    CGFloat width = imageSize.width;    CGFloat height = imageSize.height;    CGFloat targetWidth = targetSize.width;    CGFloat targetHeight = targetSize.height;    CGFloat scaleFactor = 0.0;    CGFloat scaledWidth = targetWidth;    CGFloat scaledHeight = targetHeight;    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);    if (CGSizeEqualToSize(imageSize, targetSize) == NO)     {        CGFloat widthFactor = targetWidth / width;        CGFloat heightFactor = targetHeight / height;        if (widthFactor > heightFactor)         {            scaleFactor = widthFactor; // scale to fit height        }        else        {            scaleFactor = heightFactor; // scale to fit width        }        scaledWidth  = width * scaleFactor;        scaledHeight = height * scaleFactor;        // center the image        if (widthFactor > heightFactor)        {            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;         }        else        {            if (widthFactor < heightFactor)            {                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;            }        }    }       UIGraphicsBeginImageContext(targetSize); // this will crop    CGRect thumbnailRect = CGRectZero;    thumbnailRect.origin = thumbnailPoint;    thumbnailRect.size.width  = scaledWidth;    thumbnailRect.size.height = scaledHeight;    [sourceImage drawInRect:thumbnailRect];    newImage = UIGraphicsGetImageFromCurrentImageContext();    if(newImage == nil)    {        NSLog(@"could not scale image");    }    //pop the context to get back to the default    UIGraphicsEndImageContext();    return newImage;}


An older post contains code for a method to resize your UIImage. The relevant portion is as follows:

+ (UIImage*)imageWithImage:(UIImage*)image                scaledToSize:(CGSize)newSize;{   UIGraphicsBeginImageContext( newSize );   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();   UIGraphicsEndImageContext();   return newImage;}

As far as cropping goes, I believe that if you alter the method to use a different size for the scaling than for the context, your resulting image should be clipped to the bounds of the context.


+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize {    //If scaleFactor is not touched, no scaling will occur          CGFloat scaleFactor = 1.0;    //Deciding which factor to use to scale the image (factor = targetSize / imageSize)    if (image.size.width > targetSize.width || image.size.height > targetSize.height)        if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or            scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.    UIGraphicsBeginImageContext(targetSize);     //Creating the rect where the scaled image is drawn in    CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2,                             (targetSize.height -  image.size.height * scaleFactor) / 2,                             image.size.width * scaleFactor, image.size.height * scaleFactor);    //Draw the image into the rect    [image drawInRect:rect];    //Saving the image, ending image context    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return scaledImage;}

I propose this one. Isn't she a beauty? ;)