iOS 图片压缩

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-08-09 10:41:56

iOS 图片压缩的相关文章

iOS图片压缩上传

本文实例为大家分享了iOS实现压缩图片上传功能,供大家参考,具体内容如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 7

IOS图片压缩

iPhone拍出来的照片一般大小在1-10M,我们在上传照片时,不可能上传如此大的图片到服务器,一般我们会对照片进行压缩. 常用的做法是,使用这个函数对图片压缩 UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality); 但这个函数有一个临界值,不能对图片无限制压缩,一般到当压缩比传入0.1已达到临界值了.这时如果我想把5M的图片压缩到50K是行不通的. 如果图太大,我们要求压缩的比例太大就不

iOS图片压缩处理

理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体积同样会减小. 图片“压”处理 对于“压”的功能,我们可以使用UIImageJPEGRepresentation或UIImagePNGRepresentation方法实现,如: 1 2 3 NSData *imgData = UIImageJPEGRepresentation(image, 0.5)

IOS图片压缩上传服务器终极解决方案

我最终才去的方案如下: /** * 动态发布图片压缩 * * @param source_image 原图image * @param maxSize 限定的图片大小 * * @return 返回处理后的图片 */ - (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize; 先调整分辨率,分辨率可以自己设定一个值,大于的就缩小到这分辨率,小余的就保持原本分辨率.然后在根据最终大小来设置压

iOS 图片压缩UIImage方法扩展

http://blog.csdn.net/justinjing0612/article/details/8751269 iOS自带的提供了一个API如下 [html] view plaincopy NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality); 在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentati

[iOS]图片压缩&amp;保存View为内容Img

1.图片的压缩 封装类方法 + (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize { // Create a graphics image context UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this new context, with the desired // new size [im

jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)

上传图片本身是个基本的小功能,但是到了移动端就不那么简单了,相信找到这篇文章的你一定有深深的同感. 本文实例是:在(移动端)页面中点击图片,然后选择文件,然后保存.使用Asp.net 难点一:后台获取不到FileUpload的文件 解决方案:在 form 中添加 enctype="multipart/form-data" data-ajax="false" 难点二:ios图片上传后显示为横向图片(ios横拍照片无此问题:Android无此问题) 解决方案:加载exi

iOS开发探索-图片压缩处理

介绍: 压: 指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降.缩: 指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体积同样会减小. 应用: 在实际开发中,我们经常会对图片进行处理,满足开发需求,以下介绍三种图片压缩处理: 1.压缩图片质量(图片体积减小,像素不变) 两种读取图片数据的简单方法:(1).UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数,压缩体积不是随压缩系数比例变化的.(2).UIImagePNGRepresenta

IOS开发—图片压缩/解压成Zip文件

图片压缩/解压成Zip文件 本文介绍如何将图片压缩成Zip文件,首先需要下载第三方库ZipArchive 并导入项目中. ZipArchive 库地址:https://github.com/mattconnolly/ZipArchive 一.文档结构: 二.准备工作: 1.框架导入: 2.ZipArchive.m文件使用非ARC机制 三.代码示例: // // ViewController.m // UnzipImgDemo // // Created byLotheve on 15/4/10.