IOS image Compression

Source: Internet
Author: User

iOS image compression, currently I have two types of use:

One, in-memory compression

Need to consume memory, if more pictures, may cause crash

Compressed picture quality   + (uiimage *) Reduceimage: (uiimage *) Image percent: (float) percent   {      nsdata *imagedata = uiimagejpegrepresentation (image,  percent);       uiimage *newimage = [uiimage imagewithdata: imagedata];      return newimage;  }  //Compressed picture size    +  (uiimage*) Imagewithimagesimple: (uiimage*) Image scaledtosize: (cgsize) newsize  {       // Create a graphics image context       uigraphicsbeginimagecontext (newSize);       // new  size      [image drawinrect:cgrectmake (0,0,newSize.width, Newsize.height)];      // get the new image from  The context   &nbSp;  uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext ();             // End the context       uigraphicsendimagecontext ();      // return  the new image.      return newimage;  }


Second, the use of alasset compression

#import  <AssetsLibrary/AssetsLibrary.h> #import  <imageio/imageio.h>static size_t  getassetbytescallback (void *info, void *buffer, off_t position, size_t  count)  {  ALAssetRepresentation *rep =  (__bridge id) info;     NSError *error = nil;  size_t countRead = [rep  GetBytes: (uint8_t *) buffer fromoffset:position length:count error:&error];     if  (Countread == 0 && error)  {    //  We have no way of passing this info back to the  Caller, so we log it, at least.    nslog (@ "ThumbnailForAsset : maxpixelsize: got an error reading an asset: %@ ",  error);   }    retuRn countread;} Static void releaseassetcallback (Void *info)  {  // the info here  is an alassetrepresentation which we cfretain in thumbnailforasset: Maxpixelsize:.  // this release balances that retain.  cfrelease ( info);} -  (uiimage *) Thumbnailforasset: (alasset *) Asset maxpixelsize: (NSUInteger) size {   nsparameterassert (Asset != nil);   nsparameterassert (size > 0);     ALAssetRepresentation *rep = [asset defaultRepresentation];     cgdataproviderdirectcallbacks callbacks = {    .version =  0,    .getbytepointer = null,    .releasebytepointer  = null,    .getbytesatposition = getassetbytescallback,    .releaseinfo = releaseassetcallback,  };     Cgdataproviderref provider = cgdataprovidercreatedirect (void *) CFBridgingRetain (Rep),  [rep size], &callbacks);  cgimagesourceref source =  Cgimagesourcecreatewithdataprovider (provider, null);     cgimageref imageref  = cgimagesourcecreatethumbnailatindex (source, 0,  (__bridge cfdictionaryref)  @{                                                                                                           (nsstring *) kcgimagesourcecreatethumbnailfromimagealways :  @YES,                                                                                                           (nsstring *) kcgimagesourcethumbnailmaxpixelsize : @ ( Size),                                                                                                          (nsstring *) kcgimagesourcecreatethumbnailwithtransform : @ yes,                                                                                                         });   cfrelease (source);   cfrelease (provider);     if  (!IMAGEREF)  {    return nil;  }     UIImage *toReturn = [UIImage imageWithCGImage:imageRef];     cfrelease (imageref);     return toreturn;}
#pragma  mark - UIImagePickerControllerDelegate-  (void) Imagepickercontroller: ( uiimagepickercontroller *) Picker didfinishpickingmediawithinfo: (nsdictionary *) info {   nslog (@ "info:\n%@",  info);   uiimage *image = info[ Uiimagepickercontrolleroriginalimage];  nsdata *imgdata = uiimagepngrepresentation ( image);   nslog (@ "Length1: %lu",  (unsigned long) imgdata.length);     nsurl *imageurl = info[uiimagepickercontrollerreferenceurl];  alassetslibrary  *assetsLibrary = [[ALAssetsLibrary alloc] init];  [assetsLibrary  assetforurl:imageurl resultblock:^ (Alasset *asset)  {    uiimage *image  = [self thumbnailforasset:asset maxpixelsize:1];    nsdata * Imgdata = uiimagepngrepresentation (image);  &nbsP;  nslog (@ "Length2: %lu",  (unsigned long) imgdata.length);  }  failureblock:nil];    [picker dismissviewcontrolleranimated:yes completion:^{   }];}

Using the second method, memory consumption is very low, it is suitable for the compression processing of long picture.


IOS image 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.