drawInRect: losing Quality of Image resolution

与世无争的帅哥 提交于 2019-11-29 16:49:39

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/14729021/drawinrect-losing-quality-of-image-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!