由objective-c代码创建的圆形图像始终被剪裁

时间:2015-08-08 13:41:34

标签: ios objective-c uiimage

这两天我正在研究如何用objective-c中的代码制作圆形图像。我找到了几种方法来做到这一点,但无论采用哪种方式,创建的图像都不是精确的圆形,它是切割的。请参阅以下代码和图片:

CGRect rect = CGRectMake(0.0f, 0.0f, radius*2.0f, radius*2.0f);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);     
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillEllipseInRect(context, rect);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

enter image description here

如果仔细观察图像,您会发现边缘已被切割。

最后我发现我是否更改了以下代码:

CGRect rect = CGRectMake(0.0f, 0.0f, radius*2.0f+4, radius*2.0f+4);
CGRect rectmin = CGRectMake(2.0f, 2.0f, radius*2, radius*2);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillEllipseInRect(context, rectmin);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

结果好多了,但我不认为这是一个很好的解决方案。有没有人确切知道我的第一个代码片段的问题是什么?提前谢谢。

P.S。所有截图都是从模拟器中捕获的。

enter image description here

0 个答案:

没有答案
相关问题