Algorithm-compress an image into code of the specified size

Source: Internet
Author: User
Tags scale image

Address: http://hi.baidu.com/blackj_li/item/1747aea8748ee19f151073a6

 

This is the iPhone image compression shared by cocoachina member qqn_pipi.CodeCan compress the image to a specified resolution. Post address http://www.cocoachina.com/bbs/read.php? Tid-20940.html
# Import <Foundation/Foundation. h>
@ Interface uiimage (uiimageext)
-(Uiimage *) imagebyscalingandcroppingforsize :( cgsize) targetsize;
@ End

 

# Import "uiimageext. H"
@ Implementation uiimage (uiimageext)
-(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;
}< br> @ end

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.