围绕CGContextRef进行绘制以消除像素化 [英] Drawing around CGContextRef to remove pixelation

查看:88
本文介绍了围绕CGContextRef进行绘制以消除像素化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently, I am having a graphical issue with drawing small dots.

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

I notice that in most professional Calendar applications, the events calendar indentifier is a small dot whose color is the events calendar color.

我目前在我的应用程序中需要绘制更好点的位置。这是我的意思的照片。

I am current at the point of my application where I need to draw a BETTER dot. Heres a photo of what I mean.

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

It may not be noticeable here but the colored dot is pretty pixelated when its on my phone. I would like to remove the pixelation.

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

I went through a tutorial from Ray Wenderlich's website: 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;

因此,这实际上实现了圆,但是我添加了它以在圆周围画一条小线来消除

So this actually implements the circle, but I added this to draw a small line around the circle to rid the pixelation but no go.

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

所有看起来都是这样...

All together looks like this...

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;

圆圈仍然画出,但是没有画出笔画。

The circle is still drawn but there is no "stroke" being drawn.

如果有人可以就如何解决此像素化问题提供一些建议,请发布!

If anyone would give some pointers on how this pixelation issue is solved, please post!

推荐答案

发生像素化的原因是,当您创建图像上下文时,没有考虑屏幕比例。您的代码应该是这样的(请注意第一行):

The pixelation is occurring because when you make the image context you don't take the screen scale into consideration. Your code should be something like this (note the 1st line):

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 。文档同意:

Update: As someone commented, you can pass in 0 instead of [UIScreen mainScreen].scale. The docs agree:


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

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.

这篇关于围绕CGContextRef进行绘制以消除像素化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆