无效的上下文0x0。如果您想查看回溯信息,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。

3

如果我使用这段代码,就会出现错误。

+ (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0.0, 0.0,size.width,size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

错误日志。不崩溃,与状态栏无关。

尝试查看此帖子 https://dev59.com/oFsW5IYBdhLWcg3woYYj - Nazir
我尝试了,无法解决。 - Pengfei Guo
1个回答

0

我刚刚尝试了这段代码,它正常工作。

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image = [self createImageWithColor:[UIColor blackColor] size:CGSizeMake(300, 300)];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, 300, 300);
    [self.view addSubview:imageView];
}

- (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0.0, 0.0,size.width,size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@PengfeiGuo,你能发布一下调用它的代码吗? - cleverIdiot
  • (UIImageView *)iconImageView{ if (!_iconImageView) { _iconImageView = [[UIImageView alloc] init]; _iconImageView.userInteractionEnabled = YES; _iconImageView.image = [ImageUtils createImageWithColor:[UIColor whiteColor] size:_iconImageView.size]; } return _iconImageView; }
- Pengfei Guo
你设置了ImageView的框架吗? - cleverIdiot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接