一、问题描述

下载图片,然后用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的值。

编译器警告:CGContextSaveGState: invalid context 0x0的更多相关文章

  1. 一种解决的方法:CGContextSaveGState: invalid context 0x0

    遇到这个问题找了好久答案,最后排错排出来了 CGContextSaveGState: invalid context 0x0. This is a serious error. This applic ...

  2. 【iOS】CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable

    CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SH ...

  3. CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

    最近在测试的过程中, 发现了SpringBoar的一个问题: SpringBoard[53] <Error>: CGContextTranslateCTM: invalid context ...

  4. 关于CGContextSetBlendMode: invalid context 0x0的错误

    在ios 7的模拟器中,选择一个输入框准备输入时,会触发这个错误,以下是出错详细日志: <Error>: CGContextSetBlendMode: invalid context 0x ...

  5. iOS关于CGContextSetBlendMode: invalid context 0x0的错误

    在ios 7的模拟器中,选择一个输入框准备输入时,会触发这个错误,以下是出错详细日志: <Error>: CGContextSetBlendMode: invalid context 0x ...

  6. invalid context 0x0.

    在展示图片或者做二维码的时候,若是宽或者高有一个为零的情况就会报错:CGBitmapContextCreateImage: invalid context 0x0. If you want to se ...

  7. <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

    转自这里   出 错原因:设置app的状态栏样式的使用使用了旧的方式,在info.plist里面设置了View controller- based status bar appearance为NO,默 ...

  8. dwc_otg驱动 "BUG: sleeping function called from invalid context at mm/page_alloc.c"

    方案商的开发板上otg功能只能做device,硬件看过后说没有5v供电,加上后能够识别U盘了,但是内核报了错 [ 3.264000] usb 2-1: new high-speed USB devic ...

  9. C++控制台闪回;编译器警告C4305,C4244

    这是我以前解决问题时,收集在印象笔记里的内容,为了以后整理方便,把它转移至这里.以下内容,均来自微软官方网站相关.     问题:C++控制台闪回     解决办法: 1,在程序结尾添加system( ...

随机推荐

  1. volatile修饰符

    Volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值.而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存.这样在任何时刻,两个不同的线程总是看到某个成 ...

  2. 个人作业-Week3:代码复审

    软件工程师的成长 我在上大学之前,对于软件工程师之类并无概念,并且高初中的电脑课也从未提及过写代码之类的东西,更多的都是一些教一些办公软件的使用(笑,明明电脑课总是因为老师“有事”变成其他课,根本就没 ...

  3. jsp重定向和转发

    表单提交到servlet后,servlet进行转发之后浏览器上的url为什么还是servlet的url-pattern,但是页面内容却是跳转之后页面的内容. 研究了半天之后发现这个和转发和重定向有关系 ...

  4. Android——Adapter

    Adapter——本身只是一个接口.Adapter是将数据绑定到UI界面上的桥接类.Adapter负责创建显示每个项目的子View和提供对下层数据的访问. 数据适配器作用:把复杂的数据(数组.链表.数 ...

  5. 添加webservice调用日志

    之前想用spring的AOP给webservice添加切面的,但是使用around切面后,居然调用端得不到webservice的返回结果,而且报文的详细情况也不得而知,很是尴尬,所以偷了个懒.但是该做 ...

  6. 北京培训记day5

    高级数据结构 一.左偏树&斜堆 orz黄源河论文 合并,插入,删除根节点 打标记 struct Node { int fa,l,r,w,dep } tree[Mx]; int Merge(in ...

  7. oracle--存储过程--bai

    --1 无入参最简单的存储过程 create or replace procedure hello_proc as v_name varchar2(20); v_age number; begin v ...

  8. 做web开发和测试,修改hosts指定某个域名访问某个特定的IP后,如何使hosts立即生效的方法

    本文转自SUN'S BLOG,原文地址:http://whosmall.com/post/143 hosts的配置方法: 在windows系统中,找到C:\windows\system32\drive ...

  9. Python连接MySQL

    win10.Python2.7.Pycharm import MySQLdb conn = MySQLdb.Connect( host = '127.0.0.1', port = 3306, user ...

  10. 抓取百万知乎用户信息之HttpHelper的迭代之路

    什么是Httphelper? httpelpers是一个封装好拿来获取网络上资源的工具类.因为是用http协议,故取名httphelper. httphelper出现的背景 使用WebClient可以 ...