CGContextStrokeRect is drawing only a side of rect

荒凉一梦 提交于 2019-12-13 14:06:27

问题


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!

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

回答1:


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



来源:https://stackoverflow.com/questions/8386399/cgcontextstrokerect-is-drawing-only-a-side-of-rect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!