CGContextStrokeRect只是绘制矩形的一面

我需要绘制一个充满颜色和边界的矩形…矩形正确填充颜色,但外部边框部分绘制,只是矩形的右侧绘制!

生成的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; } 

问题是,当您将self.view.frame.size传递给UIGraphicsBeginImageContext(),然后在数组中使用绘制的矩形时,它将被缩小,边框被混淆。 尝试只传递你需要的大小,即CGSizeMake(2 * 128 + 128 + 2,2 * 128 + 128 + 2)。 然后它显示确定