【问题标题】:Drawing around CGContextRef to remove pixelation围绕 CGContextRef 绘制以消除像素化
【发布时间】:2013-09-10 18:04:17
【问题描述】:

目前,我在绘制小点时遇到了图形问题。

我注意到在大多数专业日历应用程序中,事件日历标识符是一个小点,其颜色是事件日历颜色。

我在我的应用程序中需要画一个更好的点。这是我的意思的照片。

这里可能不明显,但是当它在我的手机上时,彩色圆点非常像素化。我想去除像素化。

我浏览了 Ray Wenderlich 网站上的一个教程:Ray Wenderlich Core Graphics

这是我画点的结果:

UIGraphicsBeginImageContext(cell.imageViewPic.bounds.size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(contextRef, [[event calendar] CGColor]);
CGContextFillEllipseInRect(contextRef,(CGRectMake (0.f, 0.f, cell.imageViewPic.bounds.size.width, cell.imageViewPic.bounds.size.height)));

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
cell.imageViewPic.image = image;

所以这实际上实现了圆,但我添加了它以在圆周围画一条小线以消除像素化但不行。

CGPoint startPoint = CGPointMake(cell.imageViewPic.frame.origin.x, cell.imageViewPic.frame.origin.y + cell.imageViewPic.frame.size.height - 1);
CGPoint endPoint = CGPointMake(cell.imageViewPic.frame.origin.x + cell.imageViewPic.frame.size.width - 1, cell.imageViewPic.frame.origin.y + cell.imageViewPic.frame.size.height - 1);
UIColor* color = [UIColor blackColor];
CGContextSaveGState(contextRef);
CGContextSetLineCap(contextRef, kCGLineCapSquare);
CGContextSetStrokeColorWithColor(contextRef, color.CGColor);
CGContextSetLineWidth(contextRef, 10.0);
CGContextMoveToPoint(contextRef, startPoint.x + 0.5, startPoint.y + 0.5);
CGContextAddLineToPoint(contextRef, endPoint.x + 0.5, endPoint.y + 0.5);
CGContextStrokePath(contextRef);
CGContextRestoreGState(contextRef);

整体看起来像这样......

UIGraphicsBeginImageContext(cell.imageViewPic.bounds.size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(contextRef, [[event calendar] CGColor]);
CGContextFillEllipseInRect(contextRef,(CGRectMake (0.f, 0.f, cell.imageViewPic.bounds.size.width, cell.imageViewPic.bounds.size.height)));

CGPoint startPoint = CGPointMake(cell.imageViewPic.frame.origin.x, cell.imageViewPic.frame.origin.y + cell.imageViewPic.frame.size.height - 1);
CGPoint endPoint = CGPointMake(cell.imageViewPic.frame.origin.x + cell.imageViewPic.frame.size.width - 1, cell.imageViewPic.frame.origin.y + cell.imageViewPic.frame.size.height - 1);
UIColor* color = [UIColor blackColor];
CGContextSaveGState(contextRef);
CGContextSetLineCap(contextRef, kCGLineCapSquare);
CGContextSetStrokeColorWithColor(contextRef, color.CGColor);
CGContextSetLineWidth(contextRef, 10.0);
CGContextMoveToPoint(contextRef, startPoint.x + 0.5, startPoint.y + 0.5);
CGContextAddLineToPoint(contextRef, endPoint.x + 0.5, endPoint.y + 0.5);
CGContextStrokePath(contextRef);
CGContextRestoreGState(contextRef);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
cell.imageViewPic.image = image;

圆圈仍在绘制,但没有绘制“笔划”。

如果有人能就如何解决这个像素化问题提供一些指示,请发帖!

【问题讨论】:

  • 如果您使用 100% 大小的视网膜模拟器,像素化将在模拟器中可见。

标签: ios objective-c core-graphics cgcontext


【解决方案1】:

出现像素化是因为在制作图像上下文时没有考虑屏幕比例。你的代码应该是这样的(注意第一行):

UIGraphicsBeginImageContextWithOptions(cell.imageViewPic.bounds.size, NO, [UIScreen mainScreen].scale)
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(contextRef, [[event calendar] CGColor]);
CGContextFillEllipseInRect(contextRef,(CGRectMake (0.f, 0.f, cell.imageViewPic.bounds.size.width, cell.imageViewPic.bounds.size.height)));

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
cell.imageViewPic.image = image;

更新:正如有人评论的那样,您可以传入0 而不是[UIScreen mainScreen].scale。文档同意:

应用于位图的比例因子。如果您指定值 0.0,则比例因子设置为设备主屏幕的比例因子。

【讨论】:

  • 请注意,传递0 作为比例参数与传递[UIScreen mainScreen].scale 的效果完全相同。
  • 在我的情况下,我的图像视图应该有“界限”
  • 这正是我想要的。谢谢!
【解决方案2】:

这与@Christian Di Lorenzo 的答案相同。但是这更短,更直接。

改变这个:

UIGraphicsBeginImageContext(cell.imageViewPic.bounds.size);

对此:

UIGraphicsBeginImageContextWithOptions(cell.imageViewPic.bounds.size, NO, [UIScreen mainScreen].scale);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多