【问题标题】:App crashes on iPad but not in iPad Simulator应用程序在 iPad 上崩溃,但在 iPad 模拟器中没有
【发布时间】:2012-03-18 22:30:58
【问题描述】:

在某个地方,我的应用程序开始在 iPad 上崩溃,尽管它在模拟器中仍然运行良好。我正在使用 XCode 4.3.1,并在 iOS 5.1 上进行部署。

相关代码如下。它在执行 ImageLayer 的 drawInContext: 方法时崩溃。

ImageLayer.h:

#import <QuartzCore/QuartzCore.h>

@interface ImageLayer : CALayer {}

@property (nonatomic, retain) NSString *path;

@end

ImageLayer.m:

#import "ImageLayer.h"

@implementation ImageLayer

@synthesize path;

- (void)drawInContext:(CGContextRef)ctx
{
    [super drawInContext:ctx];
    CGImageRef img = [UIImage imageWithContentsOfFile:path].CGImage;
    NSLog(@"path = %@", path);
    if (img) {
        CGRect rect = CGContextGetClipBoundingBox(ctx);
        CGContextTranslateCTM(ctx, 0.0, rect.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        rect = CGContextGetClipBoundingBox(ctx);
        CGContextDrawImage(ctx, rect, img);             // Crashes here!
        CGContextScaleCTM(ctx, 1.0, -1.0);
    }
}

@end

LayeredView.h:

#import <UIKit/UIKit.h>

@interface LayeredView : UIView {}

@end

LayeredView.m:

#import "LayeredView.h"
#import "ImageLayer.h"

@implementation LayeredView

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
        NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: @"Letter A"] stringByAppendingPathComponent:@"bg.png"];
        ImageLayer *l = [ImageLayer layer];
        l.path = path;
        l.frame = CGRectMake(0, -27, 1024, 795);
        [self.layer addSublayer:l];
        [l setNeedsDisplay];
    }
    return self;
}

崩溃日志

Incident Identifier: F0104517-289E-4B50-BBD0-5E9E0B19BAE8
CrashReporter Key:   dc4048f7e9778dfac3941d3352d4edac4a35bd47
Hardware Model:      iPad2,1
Process:         ABC [4843]
Path:            /var/mobile/Applications/88FE4107-4433-4A36-A511-0B9619B256EB/ABC.app/ABC
Identifier:      ABC
Version:         ??? (???)
Code Type:       ARM (Native)
Parent Process:  launchd [1]

Date/Time:       2012-03-16 14:38:30.326 -0700
OS Version:      iPhone OS 5.1 (9B176)
Report Version:  104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x371a132c 0x37190000 + 70444
1   libsystem_c.dylib               0x33307208 0x332ba000 + 315912
2   libsystem_c.dylib               0x33300298 0x332ba000 + 287384
3   GraphicsServices                0x333d987c 0x333d5000 + 18556
4   GraphicsServices                0x333da2bc 0x333d5000 + 21180
5   UIKit                           0x35b50c5a 0x35b1f000 + 203866
6   ABC                             0x000e045c 0xdf000 + 5212
7   ABC                             0x000e0400 0xdf000 + 5120

【问题讨论】:

  • 添加崩溃日志/XCode 提供的任何内容都可能对您有所帮助
  • 好主意。我将崩溃日志添加到第一个崩溃的线程。在 XCode 中,崩溃最终出现在机器代码中,其中 CGContextAddPath 调用 CGPathCreateMutableCopyByTransformingPath,而后者又调用 CG::Path::Path(CG::Path const&)。我相信这发生在我对上面列出的 CGContextDrawImage 的调用中。

标签: ios properties crash ios-simulator calayer


【解决方案1】:

找到了。通过改变这一行

CGImageRef img = [UIImage imageWithContentsOfFile:path].CGImage;

到

UIImage *image = [UIImage imageWithContentsOfFile:path];
CGImageRef img = image.CGImage;

一切正常。我的猜测是 ARC 只保留 CGImageRef,并处理图像,除非为它分配了一个变量。

【讨论】:

  • 我对 CGColor 有同样的问题。这个解决方案也对我有用。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
相关资源
最近更新 更多