There are two simple ways to read picture data on the iphone: UIImageJPEGRepresentation and Uiimagepngrepresentation. The UIImageJPEGRepresentation function requires two parameters: a picture's reference and a compression factor. Uiimagepngrepresentation only need picture references as parameters.
Method One:
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 Two:
. h Specific 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
and set it to be the context that is currently in use
Uigraphicsbeginimagecontext (size);
Draw a picture that changes size
[img drawinrect:cgrectmake (0, 0, Size.width, size.height)];
Create a picture that changes size from the current context
uiimage* scaledimage = Uigraphicsgetimagefromcurrentimagecontext ();
Make the current context out of the stack
Uigraphicsendimagecontext ();
Returns a new resized picture
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 < Heightfactor)
{
Thumbnailpoint.x = (targetwidth-scaledwidth) * 0.5;
}
}
Uigraphicsbeginimagecontext (targetsize); This would crop
CGRect thumbnailrect = Cgrectzero;
Thumbnailrect.origin = Thumbnailpoint;
ThumbnailRect.size.width = Scaledwidth;
ThumbnailRect.size.height = Scaledheight;
[Sourceimage Drawinrect:thumbnailrect];
NewImage = Uigraphicsgetimagefromcurrentimagecontext ();
if (newimage = = nil)
NSLog (@ "Could not scale image");
Pop the context to get back to the default
Uigraphicsendimagecontext ();
return newimage;
}
@end
Method Three: (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 mentioned is the entire content of this article, I hope you can enjoy.