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