drawInRect:失去图像分辨率的质量 [英] drawInRect: losing Quality of Image resolution

查看:276
本文介绍了drawInRect:失去图像分辨率的质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码清除图片

  CGColorRef strokeColor = [UIColor whiteColor] .CGColor; 

UIGraphicsBeginImageContext(imgForeground.frame.size);

CGContextRef context = UIGraphicsGetCurrentContext();
[imgForeground.image drawInRect:CGRectMake(0,0,imgForeground.frame.size.width,imgForeground.frame.size.height)];

CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineWidth(context,10);

CGContextSetStrokeColorWithColor(context,strokeColor);
CGContextSetBlendMode(context,kCGBlendModeClear);

CGContextBeginPath(context);
CGContextMoveToPoint(context,lastPoint.x,lastPoint.y);
CGContextAddLineToPoint(context,currentPoint.x,currentPoint.y);
CGContextStrokePath(context);

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

但我只是发现图像由于 drawInRect / p>

解决方案

$ c> UIGraphicsBeginImageContextWithOptions 而不是 UIGraphicsBeginImageContext ,以便可以指定比例因子。



例如,这将使用设备主屏幕的缩放系数:

  UIGraphicsBeginImageContextWithOptions(imgForeground.frame.size,NO,0); 


I am trying to erase image using following code

 CGColorRef strokeColor = [UIColor whiteColor].CGColor;

 UIGraphicsBeginImageContext(imgForeground.frame.size);

 CGContextRef context = UIGraphicsGetCurrentContext();
 [imgForeground.image drawInRect:CGRectMake(0, 0, imgForeground.frame.size.width, imgForeground.frame.size.height)];

 CGContextSetLineCap(context, kCGLineCapRound);
 CGContextSetLineWidth(context, 10);

 CGContextSetStrokeColorWithColor(context, strokeColor);
 CGContextSetBlendMode(context, kCGBlendModeClear);

 CGContextBeginPath(context);
 CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
 CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
 CGContextStrokePath(context);

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

But I just find out Image losing its resolution due to drawInRect.

解决方案

You should go with UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext, so that a scale factor can be specified.

For example, this will use the scale factor of the device's main screen:

UIGraphicsBeginImageContextWithOptions(imgForeground.frame.size, NO, 0);

这篇关于drawInRect:失去图像分辨率的质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆