目前,我在绘制小点时遇到图形问题。
我注意到在大多数专业日历应用程序中,事件日历标识符是一个小点,其颜色是事件日历颜色。
我目前正处于申请阶段,我需要画一个更好的点。这是我的意思的照片。
它在这里可能不明显,但在我的手机上彩色圆点像素化程度很高。我想删除像素化。
我阅读了 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;
圆仍在绘制,但没有绘制“笔画”。
如果有人对如何解决这个像素化问题提出一些建议,请发帖!
最佳答案
出现像素化是因为在制作图像上下文时没有考虑屏幕比例。你的代码应该是这样的(注意第一行):
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
。文档同意:
The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
关于ios - 围绕 CGContextRef 绘制以移除像素化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18725955/