The image compression method in IOS is summarized _ios

Source: Internet
Author: User
Tags scale image

Method One:

Copy Code code 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

Copy Code code as follows:

#import <Foundation/Foundation.h>
@interface UIImage (Uiimageext)
-(UIImage *) Scaletosize: (uiimage *) img Size: (cgsize) size;
-(UIImage *) Imagebyscalingandcroppingforsize: (cgsize) targetsize;
@end

. M specific Code

Copy Code code 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)

Copy Code code 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.

Related Article

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.