drawInRect:失去图像质量分辨率

Sha*_*tha 17 cocoa-touch quartz-graphics uiimage cgcontext ios

我试图使用以下代码擦除图像

 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();
Run Code Online (Sandbox Code Playgroud)

但我发现由于drawInRect,Image会失去分辨率.

之前 后

Nen*_*d M 42

您应该使用UIGraphicsBeginImageContextWithOptions而不是UIGraphicsBeginImageContext,以便可以指定比例因子.

例如,这将使用设备主屏幕的比例因子:

UIGraphicsBeginImageContextWithOptions(imgForeground.frame.size, NO, 0);
Run Code Online (Sandbox Code Playgroud)

  • 第三个参数可以简单地为"0",这意味着将自动选择比例(Cocoa中的常规约定) (3认同)