This article reprinted to http://blog.sina.com.cn/s/blog_6f29e81f0101tat6.html//Proportional scaling, size is how much area you want to show the graph toCgsizemake (+)
-(UIImage *) Imagecompressforsize: (UIImage *) sourceimage targetsize: (cgsize) size{
UIImage *newimage = nil;
Cgsize imageSize = sourceimage.size;
CGFloat width = imagesize.width;
CGFloat height = imagesize.height;
CGFloat targetwidth = size.width;
CGFloat targetheight = size.height;
CGFloat scalefactor = 0.0;
CGFloat scaledwidth = targetwidth;
CGFloat scaledheight = targetheight;
Cgpoint thumbnailpoint = cgpointmake (0.0, 0.0);
if (Cgsizeequaltosize (imageSize, size) = = NO) {
CGFloat widthfactor = targetwidth/width;
CGFloat heightfactor = targetheight/height;
if (Widthfactor > Heightfactor) {
Scalefactor = Widthfactor;
}
else{
Scalefactor = Heightfactor;
}
Scaledwidth = width * scalefactor;
Scaledheight = height * scalefactor;
if (Widthfactor > Heightfactor) {
Thumbnailpoint.y = (targetheight-scaledheight) * 0.5;
}else if (Widthfactor < Heightfactor) {
Thumbnailpoint.x = (targetwidth-scaledwidth) * 0.5;
}
}
Uigraphicsbeginimagecontext (size);
CGRect thumbnailrect = Cgrectzero;
Thumbnailrect.origin = Thumbnailpoint;
ThumbnailRect.size.width = Scaledwidth;
ThumbnailRect.size.height = Scaledheight;
[Sourceimage Drawinrect:thumbnailrect];
NewImage = Uigraphicsgetimagefromcurrentimagecontext ();
if (newimage = = nil) {
NSLog (@ "scale image fail");
}
Uigraphicsendimagecontext ();
return newimage;
}
Specify the width to scale proportionally
-(UIImage *) Imagecompressforwidth: (UIImage *) sourceimage targetwidth: (cgfloat) definewidth{
UIImage *newimage = nil;
Cgsize imageSize = sourceimage.size;
CGFloat width = imagesize.width;
CGFloat height = imagesize.height;
CGFloat targetwidth = definewidth;
CGFloat targetheight = height/(width/targetwidth);
Cgsize size = Cgsizemake (Targetwidth, targetheight);
CGFloat scalefactor = 0.0;
CGFloat scaledwidth = targetwidth;
CGFloat scaledheight = targetheight;
Cgpoint thumbnailpoint = cgpointmake (0.0, 0.0);
if (Cgsizeequaltosize (imageSize, size) = = NO) {
CGFloat widthfactor = targetwidth/width;
CGFloat heightfactor = targetheight/height;
if (Widthfactor > Heightfactor) {
Scalefactor = Widthfactor;
}
else{
Scalefactor = Heightfactor;
}
Scaledwidth = width * scalefactor;
Scaledheight = height * scalefactor;
if (Widthfactor > Heightfactor) {
Thumbnailpoint.y = (targetheight-scaledheight) * 0.5;
}else if (Widthfactor < Heightfactor) {
Thumbnailpoint.x = (targetwidth-scaledwidth) * 0.5;
}
}
Uigraphicsbeginimagecontext (size);
CGRect thumbnailrect = Cgrectzero;
Thumbnailrect.origin = Thumbnailpoint;
ThumbnailRect.size.width = Scaledwidth;
ThumbnailRect.size.height = Scaledheight;
[Sourceimage Drawinrect:thumbnailrect];
NewImage = Uigraphicsgetimagefromcurrentimagecontext ();
if (newimage = = nil) {
NSLog (@ "scale image fail");
}
Uigraphicsendimagecontext ();
return newimage;
}
iOS picture processing (1. Scale 2. Specify the width to scale proportionally