Summary of image compression methods in ios

  • 2020-05-07 20:30:17
  • OfStack

Method 1:


- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
 CGSize imageSize = image.size;
 CGFloat width = imageSize.width;
 CGFloat height = imageSize.height;
     
 if (width <= newSize.width && height <= newSize.height){
  return image;
 }
     
 if (width == 0 || height == 0){
  return image;
 }
     
 CGFloat widthFactor = newSize.width / width;
 CGFloat heightFactor = newSize.height / 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:

Specific code h


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

Specific code m


#import "UIImageExt.h" 
@implementation UIImage (UIImageExt) 
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ 
    // create 1 a bitmap the context 
    // And set it to the one currently in use context 
    UIGraphicsBeginImageContext(size); 
    // Draw pictures of varying sizes  
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    // From the current context Created in the 1 I changed the size of the image  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    // Make the current context Out of the stack  
    UIGraphicsEndImageContext(); 
    // Returns the new changed size image  
    return scaledImage; 

- (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; 

@end 

Method 3 :(the method used in my project)


-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.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 all the content of this article, I hope you can enjoy it.


Related articles: