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