晚上好
我即将完成我制作的这个新应用程序,在将它提交到应用程序商店之前我正在对其进行一些最终测试,但出现的一些事情确实让我很困惑。对于我的一个 View Controller ,我使用的是 UITableView,所以我实现了
-(UIView*) tableView:(UITableView*) tableView viewForHeaderInSection:(NSInteger) section
UITableViewDelegate 协议(protocol)的方法,以便我可以为标题提供我自己的自定义 View 。 (是的,我确实也遵守了 UITableViewDataSource 协议(protocol),并为它提供了所有必要的方法)
所以我编写了自己的 UIView 类并实现了 drawRect: 方法来绘制我自己的自定义 View 。当我在 iPad 6.0 和 iPhone 6.0 模拟器中运行它时,它运行得非常好。
但是,当我插入自己的 iOS 设备(运行 iOS 6)时,它崩溃并抛出 EXC_BAD_ACCESS。
我做了一些断点,发现在我的真实设备上运行应用程序时,代码只执行到这里:
// This code is the beginning of my drawRect method for my custom view
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef lightBlue = [UIColor colorWithRed:72.0/255.0
green:121.0/255.0
blue:201.0/255.0 alpha:1].CGColor;
CGColorRef cream = [UIColor colorWithRed:235.0/255.0
green:235.0/255.0
blue:235.0/255.0 alpha:1].CGColor;
CGContextSetFillColorWithColor(context,cream);
CGContextFillRect(context, _paperBox);
// the _paperBox variable was defined earlier as CGRect _paperBox
// the _paperBox variable was given a value in the -(void)layoutSubviews method
CGColorRef shadow = [UIColor colorWithWhite:.5 alpha:.5].CGColor;
CGContextSetShadowWithColor(context, CGSizeMake(0,3), 2, shadow);
CGContextSetFillColorWithColor(context, lightBlue);
// and later on I setup some code to draw a linear gradient:
NSArray colors = [NSArray arrayWithObjects:(__bridge id) lightBlue,
(__bridge id) cream,
nil];
EXC_BAD_ACCESS 恰好发生在最后一行。为什么只有在真实的 iOS 设备上运行时才会发生这种情况,而在模拟器上不会发生?
谢谢,
最佳答案
我找到了我的问题的答案,希望阅读这篇文章的其他遇到同样问题的人会发现这个回复有帮助。由于 ARC 和新的 __bridge 修饰符的发布,我的旧转换为:
(__bridge id)
在每个 CGColorRef 上,新的 ARC 术语在技术上并不“正确”,而且我对新的 __bridge 概念不太熟悉,所以我所做的修复不是为每个 UIColor 创建 CGColorRef,而是像这样将每个 UIColor 的 CGColor 属性转换为数组中的 id:
NSArray* array = [NSArray arrayWithObjects:
(id)[UIColor whiteColor]CGColor],
(id)[[UIColor colorWithRed:235.0/255.0
green:235.0/255.0 blue:235.0/255.0]CGColor],nil];
这似乎对我有用。但是,我确实建议还不熟悉新的 __bridge 修饰符的每个人在使用 ARC 时学习它们(包括我自己)。
谢谢大家!
关于iphone - Core Graphics 仅在真实设备上抛出 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14096989/