CGContextStrokeRect仅绘制矩形的一侧 [英] CGContextStrokeRect is drawing only a side of rect
问题描述
我需要绘制一个填充有颜色及其边框的矩形...
矩形已正确填充了颜色,但是部分绘制了外部边框,仅绘制了矩形的右侧!
I need to draw a rect filled with color and its border... the rect is filled with color properly but the outside border is partially drawn, just the right side of the rect is drawn!
生成的UIImage将在UITableViewCell的imageView中使用。
The generated UIImage is going to be used in a UITableViewCell's imageView.
- (UIImage *)legendItemWithColor:(UIColor *)color
{
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect outside = CGRectMake(128, 128, 128, 128);
CGRect legend = CGRectInset(outside, 1, 1);
NSLog(@"Outside: %@", NSStringFromCGRect(outside));
NSLog(@"Legend: %@", NSStringFromCGRect(legend));
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, legend);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextStrokeRect(context, outside);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsPopContext();
return img;
}
推荐答案
问题是当您通过将self.view.frame.size转换为UIGraphicsBeginImageContext(),然后在数组中使用绘制的矩形,将其缩小比例,并对边界进行模糊处理。尝试仅传递所需的大小,即CGSizeMake(2 * 128 + 128 + 2,2 * 128 + 128 + 2)。然后显示确定
The problem is when you pass self.view.frame.size to UIGraphicsBeginImageContext() and then use drawn rectangle in array, it is downscaled and the border is obfuscated. Try to pass only the size you need so, i.e. CGSizeMake(2*128+128+2,2*128+128+2). Then it displays ok
这篇关于CGContextStrokeRect仅绘制矩形的一侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!