我想压缩图像(相机/照片库),然后将其发送到服务器。我知道我可以按高度和宽度进行压缩,但我想按大小将图像压缩到固定大小(200 KB),并保持原始高度和宽度。 JPEGRepresentation中的比例因子不代表大小,仅代表压缩质量。如何在不使用任何第三方库的情况下实现这一点(压缩到固定大小)? 感谢您的帮助。图像压缩的大小 - iPhone SDK
回答
继承人一些示例代码,将尝试压缩您的图像,使其不超过或者是最大压力或最大文件大小
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);
}
上面的书面代码很棒:)。但我希望我的图像压缩到20 kb,我试着玩你的代码。但只能得到200 kb左右的大小,你能指导我怎样才能达到20 kb。 – 2013-08-12 10:17:43
@DeepK上面的解决方案是压缩原始图像。最大压缩可能不会让您期待的解决方案。因此,从压缩数据创建图像,然后再次压缩图像,直到获得所需的图像。只是一个想法。 – CodetrixStudio 2015-02-17 14:01:40
@Codetrix感谢您提出解决方案。 – 2015-02-18 04:34:56
做到这一点的一种方法是在循环中重新压缩文件,直到找到所需的大小。您可以先查找高度和宽度,然后猜测压缩因子(更大图像更多压缩),然后在压缩它之后,检查大小并再次分割差异。
我知道这不是超高效的,但我不相信有一个电话来实现特定大小的图像。
这里,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;
}
- (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 ;
}
问题是特别要求_file size_,而不是图像尺寸 – bengoesboom 2016-03-23 22:16:32
做了一些测试后,我能找到 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]);
}
这个答案是不正确和误导。请考虑删除它 – ikbal 2017-06-16 09:06:42
我把@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
}
}
- 1. 如何在iPhone SDK中获取缩放或压缩图像的大小?
- 2. 在iphone sdk中解压缩文件的最大大小?
- 3. Android - 压缩和解压缩图像的大小相同
- 4. 获取未压缩的图像大小
- 5. 压缩图像的可能性(大小)
- 6. 上传压缩图像大小-PHP
- 7. 如何压缩图像大小?
- 8. 上传压缩图像文件大小?
- 9. 如何缩小图像文件的大小而不压缩它?
- 10. 使用压缩缩小图像大小不改变尺寸
- 11. uncompress小波压缩图像
- 12. 缩小图像的大小
- 13. Facebook iOS SDK的图像压缩
- 14. 缩小大图像
- 15. 如果图像大小为GT,django-imagekit压缩图像300KB
- 16. Android - 如何压缩或缩小图像?
- 17. 缩小图像大小php
- 18. 压缩/缩小VIDEO的文件大小
- 19. 如何缩小图像iphone
- 20. 不缩小图像大小的缩略图图像
- 21. OutOfMemory异常:压缩图像会减小堆大小吗?
- 22. 将图像大小解压缩至最大尺寸
- 23. 解压缩PVRTC压缩图像格式?
- 24. 在iphone应用程序的图像视图中缩小图像大小
- 25. 如何在iphone中压缩图像?
- 26. 如何缩小图像视图中的图像大小
- 27. LZ4:压缩的压缩图像格式
- 28. Iphone SDK:每秒缩放一个图像
- 29. 图像压缩增加了PHP中的文件大小
- 30. PHP - 调整大小并压缩来自数据URI的图像
有一个在SDK中没有它的API。正如你发现的那样,你可以通过分辨率或质量来调整大小。我们遇到了同样的问题,最终我们决定调整屏幕显示并缩小85%,这极大地缩小了文件大小,但却提供了非常好的图像质量。 – 2012-02-29 22:20:33