此代码:
let rect = CGRectMake( 0.0, 0.0, 16.0, 16.0 )
let context = UIGraphicsGetCurrentContext()
UIGraphicsBeginImageContext( rect.size )
CGContextSetFillColorWithColor( context, color.CGColor )
CGContextFillRect( context, rect )
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
抛出这些错误:
MakeImage[60890] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
MakeImage[60890] <Error>: CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGRect大小似乎很好,other posts表示问题。那么还有什么不正确的设置呢?谢谢。
答案 0 :(得分:4)
您需要在开始图像上下文后获取当前上下文。
(显然,当您调用此代码时,在致电UIGraphicsBeginImageContext()
之前,没有当前上下文。因此UIGraphicsGetCurrentContext()
会返回nil。)
let rect = CGRectMake( 0.0, 0.0, 16.0, 16.0 )
UIGraphicsBeginImageContext( rect.size )
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor( context, color.CGColor )
CGContextFillRect( context, rect )
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
答案 1 :(得分:0)
我的不好,这两个只是错误的方式:
UIGraphicsBeginImageContext( rect.size )
let context = UIGraphicsGetCurrentContext()