iOS的图片压缩,目前我在用的有两种:
一、在内存中压缩
需要消耗内存,如果图片较多,可能会造成crash
//压缩图片质量 +(UIImage *)reduceImage:(UIImage *)image percent:(float)percent { NSData *imageData = UIImageJPEGRepresentation(image, percent); UIImage *newImage = [UIImage imageWithData:imageData]; return newImage; } //压缩图片尺寸 + (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 UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; }
二、利用ALAsset压缩
#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); NSLog(@"length2: %lu", (unsigned long)imgData.length); } failureBlock:nil]; [picker dismissViewControllerAnimated:YES completion:^{ }]; }
使用第二种方法内存占用率很低,很适合做多张图片的压缩处理。
时间: 2024-10-12 19:13:58