iphone - Core Graphics 仅在真实设备上抛出 EXC_BAD_ACCESS

标签 iphone objective-c ios uitableview exc-bad-access

晚上好

我即将完成我制作的这个新应用程序,在将它提交到应用程序商店之前我正在对其进行一些最终测试,但出现的一些事情确实让我很困惑。对于我的一个 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/

相关文章:

iphone - iOS Dev - 如何将同一应用程序的不同版本部署到同一设备上?

iphone - 如何让 NSTimer 在 iOS 7 的后台保持事件状态?

objective-c - Xcode:与错误代码 -1 失去联系

ios - 如何从 ios 中的静态库中获取 .m 文件

ios - 对角动画 UIImageView

ios - 快速解析: can not retrieve the value of a bool in _User class

ios - GCDWebServer - 文档中的静态文件

iphone - 如何在 iOS Gamecenter match.participants 中找到已满的座位?

iphone - 远程唤醒应用程序,比如从蓝牙设备

objective-c - 在 iOS sdk 中解析本地 xml 文件