iOS 图片压缩
来源:互联网 发布:计算机二级vb培训 编辑:程序博客网 时间:2024/06/06 03:22
发帖的时候需要用到从相册中选取图片然后上传,由于每次上传图片的最大数量为9张,所以需要对图片进行压缩。开始时用了以前经常用的压缩的方法:
上面的方法比较常见,可是需要加载到内存中来处理图片,当图片数量多了的时候就会收到内存警告,程序崩溃。研究半天终于在一篇博客中找到了解决方法:
采用上面的方法之后内存占用率很低!
- //压缩图片质量
- +(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;
- }
上面的方法比较常见,可是需要加载到内存中来处理图片,当图片数量多了的时候就会收到内存警告,程序崩溃。研究半天终于在一篇博客中找到了解决方法:
- static size_t getAssetBytesCallback(voidvoid *info, voidvoid *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.
- NDDebug(@"thumbnailForAsset:maxPixelSize: got an error reading an asset: %@", error);
- }
- return countRead;
- }
- static void releaseAssetCallback(voidvoid *info) {
- // The info here is an ALAssetRepresentation which we CFRetain in thumbnailForAsset:maxPixelSize:.
- // This release balances that retain.
- CFRelease(info);
- }
- // Returns a UIImage for the given asset, with size length at most the passed size.
- // The resulting UIImage will be already rotated to UIImageOrientationUp, so its CGImageRef
- // can be used directly without additional rotation handling.
- // This is done synchronously, so you should call this method on a background queue/thread.
- - (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((voidvoid *)CFBridgingRetain(rep), [rep size], &callbacks);
- CGImageSourceRef source = CGImageSourceCreateWithDataProvider(provider, NULL);
- CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef) @{
- (NSString *)kCGImageSourceCreateThumbnailFromImageAlways : @YES,
- (NSString *)kCGImageSourceThumbnailMaxPixelSize : [NSNumber numberWithInt:size],
- (NSString *)kCGImageSourceCreateThumbnailWithTransform : @YES,
- });
- CFRelease(source);
- CFRelease(provider);
- if (!imageRef) {
- return nil;
- }
- UIImage *toReturn = [UIImage imageWithCGImage:imageRef];
- CFRelease(imageRef);
- return toReturn;
- }
采用上面的方法之后内存占用率很低!
0 0
- IOS 图片压缩
- ios--图片压缩/缩放
- IOS 图片压缩
- ios图片压缩
- IOS 图片压缩
- iOS 图片压缩
- IOS 图片压缩
- ios 图片压缩
- ios 图片压缩
- ios 图片压缩
- ios 图片压缩
- ios图片压缩
- ios 图片压缩
- iOS 图片压缩
- IOS 图片压缩
- 图片压缩 ios 上传
- ios 压缩图片
- iOS 图片压缩
- Android学习笔记--什么是回调
- 1040. 有几个PAT(25)-PAT乙级真题
- Android代码的组织结构
- AFHTTPRequestOperationManager找不到的原因
- C语言中位移问题
- iOS 图片压缩
- 2016 Multi-University Training Contest 2 题解(慢慢补)
- Freemarker宏
- Codeforces Beta Round #94 (Div. 1 Only)String
- django 直接执行mysql 语句
- Java8 特性—— Lambda 表达式
- LTE -DCI3和功率调整
- iOS地图上WGS84、GCJ-02、BD-09互转解决方案
- 1039. 到底买不买(20)-PAT乙级真题