我想要压缩图片(来自相机/照片库),并将其发送到服务器。我知道可以通过高度和宽度来压缩,但我希望仅通过大小将图像压缩到固定的大小(200 KB),同时保留原始的高度和宽度。JPEGRepresentation中的比例因子不代表大小,仅表示压缩质量。如何在不使用任何第三方库的情况下实现这一目标(压缩到固定大小)?感谢任何帮助。
我想要压缩图片(来自相机/照片库),并将其发送到服务器。我知道可以通过高度和宽度来压缩,但我希望仅通过大小将图像压缩到固定的大小(200 KB),同时保留原始的高度和宽度。JPEGRepresentation中的比例因子不代表大小,仅表示压缩质量。如何在不使用任何第三方库的情况下实现这一目标(压缩到固定大小)?感谢任何帮助。
这里是一些示例代码,将尝试为您压缩图像,以使其不超过最大压缩或最大文件大小。
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);
}
一种方法是通过循环重新压缩文件,直到找到所需的大小。您可以先找到高度和宽度,并猜测压缩因子(更大的图像更多压缩),然后在压缩后检查大小,并再次分割差异。
我知道这不是非常有效,但我不相信有一个单一的调用可以实现特定大小的图像。
在这里,JPEGRepresentation占用的内存非常大,如果我们在循环中使用它,那么内存消耗会非常高。因此,请使用以下代码,图像大小不会超过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;
}
Swift 4:
extension UIImage {
func compressTo(bytes: Int) -> UIImage {
var compression: CGFloat = 0.9
let maxCompression: CGFloat = 0.1
let maxSize: Int = bytes * 1024
var imageData = jpegData(compressionQuality: compression)!
while imageData.count > maxSize && compression > maxCompression {
compression -= 0.1
imageData = jpegData(compressionQuality: compression)!
}
return UIImage(data: imageData)!
}
}
我采用了@kgutteridge的答案,使用递归为Swift 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的所有值时这种关系是线性的,所以我创建了一个算法,尝试将图像始终压缩到特定的值。
//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]);
}
- (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 ;
}