SoFunction
Updated on 2025-04-13

Summary of methods for compression of images in ios

Method 1:

Copy the codeThe code is as follows:

- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
 CGSize imageSize = ;
 CGFloat width = ;
 CGFloat height = ;
     
 if (width <= && height <= ){
  return image;
 }
     
 if (width == 0 || height == 0){
  return image;
 }
     
 CGFloat widthFactor = / width;
 CGFloat heightFactor = / height;
 CGFloat scaleFactor = (widthFactor<heightFactor?widthFactor:heightFactor);
     
 CGFloat scaledWidth = width * scaleFactor;
 CGFloat scaledHeight = height * scaleFactor;
 CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);
     
    UIGraphicsBeginImageContext(targetSize);
    [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Method 2:

.h specific code

Copy the codeThe code is as follows:

#import <Foundation/> 
@interface UIImage (UIImageExt) 
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size; 
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize; 
@end 

.m specific code

Copy the codeThe code is as follows:

#import "" 
@implementation UIImage (UIImageExt) 
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ 
// Create a bitmap context
// and set it to the context currently in use
    UIGraphicsBeginImageContext(size); 
// Draw pictures that change the size
    [img drawInRect:CGRectMake(0, 0, , )]; 
// Create a changed size image from the current context
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
// Make the current context out of the stack
    UIGraphicsEndImageContext(); 
// Return to the new image after changing size
    return scaledImage; 

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 

    UIImage *sourceImage = self; 
    UIImage *newImage = nil; 
    CGSize imageSize = ; 
    CGFloat width = ; 
    CGFloat height = ; 
    CGFloat targetWidth = ; 
    CGFloat targetHeight = ; 
    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) 
        { 
            = (targetHeight - scaledHeight) * 0.5; 
        } 
        else 
            if (widthFactor < heightFactor) 
            { 
                = (targetWidth - scaledWidth) * 0.5; 
            } 
    } 
    UIGraphicsBeginImageContext(targetSize); // this will crop 
    CGRect thumbnailRect = CGRectZero; 
    = thumbnailPoint; 
      = scaledWidth; 
    = 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; 

@end 

Method 3: (Methods used in my project)

Copy the codeThe code is as follows:

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
    CGSize imageSize = ;
    CGFloat width = ;
    CGFloat height = ;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

The above is the entire content of this article, I hope you like it.