编译器警告:CGContextSaveGState: invalid context 0x0

一、问题描述

下载图片,然后用Quartz2D绘制缩放的图片,运行无法显示图片并且编译器警告:

Aug 18 21:41:50  02_计算UITableViewCell的行高[16777] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Aug 18 21:41:50  02_计算UITableViewCell的行高[16777] <Error>: CGContextSetBlendMode: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Aug 18 21:41:50  02_计算UITableViewCell的行高[16777] <Error>: CGContextSetAlpha: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Aug 18 21:41:50  02_计算UITableViewCell的行高[16777] <Error>: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Aug 18 21:41:50  02_计算UITableViewCell的行高[16777] <Error>: CGContextScaleCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Aug 18 21:41:50  02_计算UITableViewCell的行高[16777] <Error>: CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Aug 18 21:41:50  02_计算UITableViewCell的行高[16777] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

二、问题分析

有人说因为设置app的状态栏样式的使用了旧的方式,一般式iOS6的时候使用这种方式,iOS7、8也兼容,但是到了iOS9就报了警告。在info.plist里面设置了View controller-based status bar appearance为NO,要设置为YES。

View controller-based status bar appearance设置YES后,问题还是没解决。

后来发现只要执行以下代码都会警告:

 1 - (void)setImageView
 2 {
 3 NSString *url = @"http://ww3.sinaimg.cn/large/005P1ePojw1f6xzt7o6saj30go4w8wna.jpg";  4 // 设置图片  5 __weak typeof(self) weakSelf = self;  6 [self.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {  7 // 开启图形上下文  8 UIGraphicsBeginImageContextWithOptions(weakSelf.size, YES, 0.0);  9 // 将下载完的image对象绘制到图形上下文 10 CGFloat width = weakSelf.size.width; 11 CGFloat height = width * 520 / 300; 12 [image drawInRect:CGRectMake(0, 0, width, height)]; 13 // 获得图片 14 weakSelf.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 15 // 结束图形上下文 16  UIGraphicsEndImageContext(); 17  }]; 18 }

 

 后来发现问题出现在这处: UIGraphicsBeginImageContextWithOptions(weakSelf.size, YES, 0.0);

因为weakSelf.size的值CGSizeMake(0, 0),导致错误。

三、问题解决

重新设置self.size的值

 

转载于:https://www.cnblogs.com/Zev_Fung/p/5785444.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值