[iOS]CoreGraph在64位模式下与NSString drawInRect冲突

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)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值