iOS SDK支持64bit后,大部分代码可以直接无缝转换并通过编译,但是这些代码在运行时可能会有所差异,所以开发时需要特别注意,Cocoa-Charts在开发过程中就发现了这类问题。问题现象:CoreGraph绘图方法与NSString的drawInRect方法在64Bit下存在着冲突,64Bit下调用drawInRect之后会导致CGContext中的path被清空从而使CGContextStrokePath不进行任何绘图操作,而32Bit下没有任何问题。
问题代码:
CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetLineWidth(context, 0.5f);CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);//代码省略//在控件内绘制一条直线CGContextMoveToPoint(context, 0.f, 0.f);CGContextAddLineToPoint(context, 100.f, 100.f);//在控件内显示指定的文本NSString string = @“TEST”;[string drawInRect:CGRectMake(0, 0, 100, 20) withFont: [UIFont systemFontOfSize:14.f] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];//代码省略CGContextStrokePath(context);CGContextFillPath(context);解决办法:将代码分割,将使用CoreGraph绘图方法的代码段与调用NSString的drawInRect方法的代码段分别书写。
修改后的代码:
CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetLineWidth(context, 0.5f);CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);//代码省略//在控件内绘制一条直线CGContextMoveToPoint(context, 0.f, 0.f);CGContextAddLineToPoint(context, 100.f, 100.f);//代码省略CGContextStrokePath(context);CGContextFillPath(context);//在控件内显示指定的文本NSString string = @“TEST”;[string drawInRect:CGRectMake(0, 0, 100, 20) withFont: [UIFont systemFontOfSize:14.f] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];最后感谢BUG的发现者:Marco Almeida(http://www.marcoios.com/MarcoiOS/index.html)
