我写了这个UIImage的简单扩展,从预定义的颜色和大小开始绘制图像。
这是代码:
extension UIImage {
class func image(color:UIColor, size:CGSize)->UIImage {
UIGraphicsBeginImageContextWithOptions(size, true, 2.0);
let context = UIGraphicsGetCurrentContext()
let rect = CGRectMake(0, 0, size.width, size.height)
color.set()
CGContextFillRect(context, rect)
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
如果我在控制器的ViewDidLoad方法中启动此代码,我会收到此错误
Oct 31 16:48:02 iPhone Budjet_v2[3387] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Oct 31 16:48:02 iPhone Budjet_v2[3387] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Oct 31 16:48:02 iPhone Budjet_v2[3387] <Error>: CGContextFillRects: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
fatal error: unexpectedly found nil while unwrapping an Optional value
我无法理解我的代码有什么问题。
答案 0 :(得分:1)
这似乎是iOS 7中引入的错误。答案here中列出了创建此错误的完整方法列表。
此问题的建议行动方案是向CGPostError添加一个符号断点,以便在发生此错误时分析堆栈,以确定触发此错误的内容。
此错误可能由多种情况触发,如果没有进一步研究,无法确定哪种情况会触发错误。
祝你好运!答案 1 :(得分:1)
if (!(CGSizeEqualToSize(size, CGSizeZero))){
// your drawing code....
}else{
return nil;
}
(这个ObjC很明显,道歉,我还不是一个有成就的快速甩手...... 我怀疑你的问题是大小的上下文(0,0)