IOS median compression screenshot code

Source: Internet
Author: User

IOS moderately compressed code

Compress and enlarge an image according to the desired size, and finally extract the required size. I don't know if it is clear. That's what it means anyway!

+ (UIImage *) compressImageWith :( UIImage *) image width :( float) width height :( float) height
{
Float imageWidth = image. size. width;
Float imageHeight = image. size. height;

Float widthScale = imageWidth/width;
Float heightScale = imageHeight/height;

// Create a bitmap context
// Set it to the context currently in use
UIGraphicsBeginImageContext (CGSizeMake (width, height ));

If (widthScale> heightScale ){
[Image drawInRect: CGRectMake (0, 0, imageWidth/heightScale, height)];
}
Else {
[Image drawInRect: CGRectMake (0, 0, width, imageHeight/widthScale)];
}

// Create an image with the size changed from the current context
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
[NewImage retain];
// Make the current context output from the stack
UIGraphicsEndImageContext ();

Return newImage;

}

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.