Summary of image compression methods in ios

Source: Internet
Author: User
Tags scale image

Summary of image compression methods in ios

There are two simple methods to read image data on the Iphone: UIImageJPEGRepresentation and UIImagePNGRepresentation. UIImageJPEGRepresentation require two parameters: image reference and compression coefficient. UIImagePNGRepresentation only needs image reference as the parameter.

Method 1:

The Code is as follows:

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

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 code

The Code is as follows:

# Import

@ Interface UIImage (UIImageExt)

-(UIImage *) scaleToSize :( UIImage *) img size :( CGSize) size;

-(UIImage *) imageByScalingAndCroppingForSize :( CGSize) targetSize;

@ End

. M specific code

The Code is as follows:

# Import "UIImageExt. h"

@ Implementation UIImage (UIImageExt)

-(UIImage *) scaleToSize :( UIImage *) img size :( CGSize) size {

// Create a bitmap context

// Set it to the context currently in use

UIGraphicsBeginImageContext (size );

// Draw an image of varying size

[Img drawInRect: CGRectMake (0, 0, size. width, size. height)];

// Create an image with the size changed from the current context

UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext ();

// Make the current context output from the stack

UIGraphicsEndImageContext ();

// Returns a new image with a changed size.

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

{

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 (@ "cocould not scale image ");

// Pop the context to get back to the default

UIGraphicsEndImageContext ();

Return newImage;

}

@ End

Method 3: (the method used in my project)

The Code is as follows:

-(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 will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.