2012-02-29 113 views
24

我想压缩图像(相机/照片库),然后将其发送到服务器。我知道我可以按高度和宽度进行压缩,但我想按大小将图像压缩到固定大小(200 KB),并保持原始高度和宽度。 JPEGRepresentation中的比例因子不代表大小,仅代表压缩质量。如何在不使用任何第三方库的情况下实现这一点(压缩到固定大小)? 感谢您的帮助。图像压缩的大小 - iPhone SDK

+0

有一个在SDK中没有它的API。正如你发现的那样,你可以通过分辨率或质量来调整大小。我们遇到了同样的问题,最终我们决定调整屏幕显示并缩小85%,这极大地缩小了文件大小,但却提供了非常好的图像质量。 – 2012-02-29 22:20:33

回答

62

继承人一些示例代码,将尝试压缩您的图像,使其不超过或者是最大压力或最大文件大小

CGFloat compression = 0.9f; 
CGFloat maxCompression = 0.1f; 
int maxFileSize = 250*1024; 

NSData *imageData = UIImageJPEGRepresentation(yourImage, compression); 

while ([imageData length] > maxFileSize && compression > maxCompression) 
{ 
    compression -= 0.1; 
    imageData = UIImageJPEGRepresentation(yourImage, compression); 
} 
+2

上面的书面代码很棒:)。但我希望我的图像压缩到20 kb,我试着玩你的代码。但只能得到200 kb左右的大小,你能指导我怎样才能达到20 kb。 – 2013-08-12 10:17:43

+0

@DeepK上面的解决方案是压缩原始图像。最大压缩可能不会让您期待的解决方案。因此,从压缩数据创建图像,然后再次压缩图像,直到获得所需的图像。只是一个想法。 – CodetrixStudio 2015-02-17 14:01:40

+0

@Codetrix感谢您提出解决方案。 – 2015-02-18 04:34:56

3

做到这一点的一种方法是在循环中重新压缩文件,直到找到所需的大小。您可以先查找高度和宽度,然后猜测压缩因子(更大图像更多压缩),然后在压缩它之后,检查大小并再次分割差异。

我知道这不是超高效的,但我不相信有一个电话来实现特定大小的图像。

1

这里,JPEGRepresentation是相当耗费内存,如果我们使用在循环中,所以它是非常高的内存消耗。因此,使用下面的代码& ImageSize不会超过200KB。

UIImage* newImage = [self captureView:yourUIView]; 


- (UIImage*)captureView:(UIView *)view { 
CGRect rect = view.bounds; 
UIGraphicsBeginImageContext(rect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

[view.layer renderInContext:context]; 
UIImage* img = [UIImage alloc]init]; 
img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
NSLog(@"img=%@",img); 
return img; 
} 
-1
- (UIImage *)resizeImageToSize:(CGSize)targetSize 
{ 
    UIImage *sourceImage = captureImage; 
    UIImage *newImage = nil; 

    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 

    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 

    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) { 

     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor < heightFactor) 
      scaleFactor = widthFactor; 
     else 
      scaleFactor = heightFactor; 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // make image center aligned 
     if (widthFactor < heightFactor) 
     { 
      thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
     else if (widthFactor > heightFactor) 
     { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
    } 

    UIGraphicsBeginImageContext(targetSize); 
    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 
    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    if(newImage == nil) 
     NSLog(@"could not scale image"); 

    return newImage ; 
} 
+0

问题是特别要求_file size_,而不是图像尺寸 – bengoesboom 2016-03-23 22:16:32

0

做了一些测试后,我能找到 A relationship between image size and compression value。由于这种关系对于压缩小于1的所有值都是线性的,因此我创建了一种算法来尝试将图像压缩到某个值。

//Use 0.99 because at 1, the relationship between the compression and the file size is not linear 
NSData *image = UIImageJPEGRepresentation(currentImage, 0.99); 
float maxFileSize = MAX_IMAGE_SIZE * 1024; 

//If the image is bigger than the max file size, try to bring it down to the max file size 
if ([image length] > maxFileSize) { 
    image = UIImageJPEGRepresentation(currentImage, maxFileSize/[image length]); 
} 
+2

这个答案是不正确和误导。请考虑删除它 – ikbal 2017-06-16 09:06:42

0

我把@kgutteridge的答案,并提出了用递归雨燕3.0类似的解决方案:

extension UIImage { 
    static func compress(image: UIImage, maxFileSize: Int, compression: CGFloat = 1.0, maxCompression: CGFloat = 0.4) -> Data? { 

     if let data = UIImageJPEGRepresentation(image, compression) { 

      let bcf = ByteCountFormatter() 
      bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only 
      bcf.countStyle = .file 
      let string = bcf.string(fromByteCount: Int64(data.count)) 
      print("Data size is: \(string)") 

      if data.count > (maxFileSize * 1024 * 1024) && (compression > maxCompression) { 
       let newCompression = compression - 0.1 
       let compressedData = self.compress(image: image, maxFileSize: maxFileSize, compression: newCompression, maxCompression: maxCompression) 
       return compressedData 
      } 

      return data 
     } 

     return nil 
    } 
}