CGContextStrokeRect只绘制了rect的一面

时间:2011-12-05 13:59:08

标签: iphone objective-c ios sdk core-graphics

我需要绘制一个充满颜色及其边框的矩形...... rect正确填充颜色,但部分绘制了外边框,只绘制了rect的右侧!

生成的UIImage将用于UITableViewCell的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;
}

1 个答案:

答案 0 :(得分:2)

问题是当你将self.view.frame.size传递给UIGraphicsBeginImageContext()然后在数组中使用绘制的矩形时,它会缩小尺寸并对边框进行模糊处理。尝试只传递你需要的大小,即CGSizeMake(2 * 128 + 128 + 2,2 * 128 + 128 + 2)。然后它显示确定