绘制CGContextRef以消除像素化
目前,我正在绘制小点graphics问题。
我注意到,在大多数专业日历应用程序中,事件日历标识是一个小点,其颜色是事件日历颜色。
我目前在我的应用程序点,我需要画一个更好的点。 下面是我的意思的照片。
在这里可能不太明显,但是在我的手机上,这个彩色的点是非常像素的。 我想删除像素化。
我经历了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;
圆仍然绘制,但没有绘制“中风”。
如果有人会提供一些关于如何解决这个像素问题的指针,请发表!
像素化正在发生,因为当您创build图像上下文时,不会考虑屏幕比例。 你的代码应该是这样的(注意第一行):
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;
更新: [UIScreen mainScreen].scale
人评论,你可以传入0
而不是[UIScreen mainScreen].scale
。 文档同意:
适用于位图的比例因子。 如果您指定的值为0.0,则缩放系数将设置为设备主屏幕的缩放系数。
这与@Christian Di Lorenzo的答案是一样的。但是这个更简单一些。
改变这个:
UIGraphicsBeginImageContext(cell.imageViewPic.bounds.size);
对此:
UIGraphicsBeginImageContextWithOptions(cell.imageViewPic.bounds.size, NO, [UIScreen mainScreen].scale);