【发布时间】:2016-01-26 11:48:30
【问题描述】:
我正在开发一个图像绘图应用程序,用户可以在其中绘制图像。 但是在图像上绘制一段时间后,图像文件大小急剧增加。 (例如,如果绘制 1.6 mb 的 jpeg 图像一段时间后,它会变为 2.3 mb。大小会根据草图的数量而增加) 如果我在 Mac 中使用“预览”在图像上绘制相同数量的草图,则根本不会影响图像文件的大小。以下是编辑前后的示例图片(由于此处的图片上传限制为 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