Object-c image compression-pixel compression and non-pixel compression

Source: Internet
Author: User

/* * @brief compress picture @Fire * * @param originimage Original image * @param pc is pixel compressed * @param the maximum pixel length and width of maxpixel after compression; Pc=no, this parameter is invalid. * @param JC is JPEG compressed * @param maxkb Picture maximum volume, in kilobytes, this parameter is invalid when Jc=no. * * @return return picture of NSData */-(nsdata*) Compressimage: (uiimage*) originimage pixelcompress: (BOOL) PC Maxpixel: (cgfloat) Maxpixel jpegcompress: (BOOL) JC maxsize_kb: (cgfloat) maxkb;




-(nsdata*) Compressimage: (uiimage*) originimage pixelcompress: (BOOL) PC Maxpixel: (cgfloat) Maxpixel JPEGCompress: ( BOOL) JC maxsize_kb: (cgfloat) maxkb{/* Compression policy: Supports up to 921,600 pixel pixel compression: (number of pixel points) when the picture aspect ratio is less than 3:1 or 1:3, the picture            The length and width are up to maxpixel pixels, and when the picture aspect ratio is between 3:1 and 1:3, the picture pixels are compressed to within 921600 pixels; JPEG compression: (Adjusts the storage volume per pixel) By default compression ratio 0.99;     If the compressed data is still greater than image_max_bytes, then the compression ratio will be compressed to image_max_bytes the picture below.        Policy tuning: No pixel compression, or increased maxpixel, the smaller the pixel loss.     Use lossless compression, or increase image_max_bytes.     Note: When the JEPG compression ratio is 0.99, the image volume can be compressed to 0.40 times times the original.    */UIImage * scopedimage = nil;    NSData * data = nil;    CGFloat maxbytes = maxkb * 1024;    if (originimage = = nil) {return nil; } if (PC = = YES) {//pixel compression//Pixel count is maxpixel*maxpixel cgfloat photoratio = OriginImage.size.heig        Ht/originimage.size.width; if (Photoratio < 0.3333f) {//solve the problem of wide-length graphs cgfloat finalwidth = sqrt (maxpix El*maxpixel/photoratio);        Scopedimage = [Encodeutil convertimage:originimage scope:max (Finalwidth, Maxpixel)]; } else if (Photoratio <= 3.0f) {//Solve high-length graph problem scopedimage = [Encodeut        Il convertimage:originimage scope:maxpixel]; } else {//general picture CGFloat finalheight = sqrt (maxpixel*maxpixel*photorat            IO);        Scopedimage = [Encodeutil convertimage:originimage scope:max (Finalheight, Maxpixel)];    }} else {//do not pixel compression scopedimage = originimage;        } [Scopedimage retain];        if (JC = = YES) {//jpeg Compressed data = UIImageJPEGRepresentation (Scopedimage, 0.8);    NSLog (@ "Data compress with ratio (0.9):%d KB", data.length/1024);        } else {data = UIImageJPEGRepresentation (Scopedimage, 1.0);    NSLog (@ "Data compress:%d KB", data.length/1024);        } [Scopedimage release]; return data;}


ENCODEUTIL.M implementation


+ (UIImage *) Convertimage: (UIImage *) origimage scope: (cgfloat) scope{UIImage *image = nil;    Cgsize size = origimage.size;    if (Size.width < scope && Size.Height < scope) {//do nothing image = Origimage;        } else {cgfloat length = size.width;        if (Size.width < size.height) {length = Size.Height;        } cgfloat f = scope/length;        Nsinteger NW = size.width * F;        Nsinteger NH = size.height * F;        if (NW > scope) {NW = scope;        } if (NH > Scope) {NH = scope;                } cgsize newSize = Cgsizemake (NW, NH);//Cgsize newSize = Cgsizemake (size.width*f, size.height*f);        Uigraphicsbeginimagecontext (newSize);        Uigraphicsbeginimagecontextwithoptions (NewSize, NO, 0.0f); Tell the old image to draw in this new context, with the desired//new size [Origimage Drawinrect:cgrec Tmake (0, 0, Newsize.width,Newsize.height)];        Get the new image from the context image = Uigraphicsgetimagefromcurrentimagecontext ();     Uigraphicsendimagecontext (); } return image;


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Object-c image compression-pixel compression and non-pixel compression

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.