【问题标题】:iOS - Drawing on a UIImage increases the image file size drasticallyiOS - 在 UIImage 上绘图会大大增加图像文件的大小
【发布时间】:2016-01-26 11:48:30
【问题描述】:

我正在开发一个图像绘图应用程序,用户可以在其中绘制图像。 但是在图像上绘制一段时间后,图像文件大小急剧增加。 (例如,如果绘制 1.6 mb 的 jpeg 图像一段时间后,它会变为 2.3 mb。大小会根据草图的数量而增加) 如果我在 Mac 中使用“预览”在图像上绘制相同数量的草图,则根本不会影响图像文件的大小。以下是编辑前后的示例图片(由于此处的图片上传限制为 2 mb,我只做了少量草图)

Image before editing - 1.6 mb

Image after editing - 2 mb

不确定是什么导致图像文件大小急剧增加。 PFB 代码为 sn-p。

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    currentLocation = [touch locationInView:containerImgVIew];
    UIGraphicsBeginImageContext(drawingView.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [drawingView.image drawInRect:CGRectMake(0, 0, drawingView.frame.size.width, drawingView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, slider.value);
    CGContextBeginPath(ctx);

    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);

    drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
}

 //  touchesEnded method implemented same as touchesMoved

- (IBAction)saveButtonTapped:(id)sender
{
   UIGraphicsBeginImageContextWithOptions(_originalImageSize, YES, containerImgVIew.image.scale);

   CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
   [containerImgVIew.image drawAtPoint:CGPointZero];

   CGFloat scale = _originalImageSize.width / drawingView.frame.size.width;
   CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
   [drawingView.layer renderInContext:UIGraphicsGetCurrentContext()];

   UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   NSData *jpgData = UIImageJPEGRepresentation(viewImage, 1);
}

绘制一段时间后的图像文件大小远大于原始图像大小。我尝试使用 png 图像,但结果相同。

【问题讨论】:

    标签: ios objective-c image


    【解决方案1】:

    您可以在saveButtonTapped

    中为 JPEG 编码选择较低质量
    NSData* jpgData = UIImageJPEGRepresentation(viewImage, 0.8);
    

    0.8 之类的东西不应该太明显,应该确实可以改善文件大小。

    【讨论】:

    • @Manoj 告诉我是否有帮助
    • 感谢 Piyush 的回复。但是我已经尝试过了,并且观察到质量也没有太大变化。但要求是我们不能使用压缩。有什么办法可以做到吗?
    • 啊,好吧,让我再找一些其他的选择
    • iPhone 上的照片存储为 JPEG 文件。 JPEG 文件总是被压缩的。 “...不得使用压缩”是不可能的。
    • @alexkent - 即使我们使用 UIImageJPEGRepresentation(viewImage, 1),JPEG 文件也会被压缩吗?
    【解决方案2】:

    iPhone 上的照片存储为 JPEG 文件。 JPEG 文件总是被压缩的。 “...不得使用压缩”不是可实现的要求。

    使用设备相机拍摄的照片以大约 0.75 的质量存储(我估计)。当您从 JPEG 实例化 UIImage 时,磁盘上的压缩数据被解压缩,并且在您编辑它时 RAM 中存在完整大小的未压缩图像。此内存中的副本经过一次 JPEG 压缩和解压缩后略有下降。

    您对内存中的未压缩图像进行编辑。保存更改时,将图像数据再次压缩为 JPEG。您致电UIImageJPEGRepresentation 并选择压缩级别。选择 0.75 左右的值应该会给您提供与您加载的图像相似的文件大小。

    每次使用 JPEG 格式压缩图像时,都会稍微降低它的质量。在高 JPEG 质量设置下,这些降级是非常难以察觉的。这种迭代退化只是使用 JPEG 等有损压缩格式的一部分。

    【讨论】:

    • 感谢@alexkent 的解释。还有一个疑问。当我使用 UIImageJPEGRepresentation(image, 0.99) 甚至没有编辑并将数据直接保存到磁盘时,图像文件大小几乎变成了一半。这是理想的行为吗?
    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多