Ios UIScrollView中的CGLayer崩溃

Ios UIScrollView中的CGLayer崩溃,ios,performance,scroll,draw,layer,Ios,Performance,Scroll,Draw,Layer,我有一个从UIScrollView继承的类。drawRect函数使用CGLayer缓存图形,以便更快地滚动。不幸的是,每次我尝试滚动视图时,代码都会崩溃 只要在iOS模拟器中单击并拖动,它就会崩溃,并出现以下错误: 2012-08-04 19:11:09.687 scrolllayertest[3197:c07]-[\uu\NSCFType animationKeys]:发送到实例0x682aa20的无法识别的选择器 2012-08-04 19:11:09.689 scrolllayertest

我有一个从UIScrollView继承的类。drawRect函数使用CGLayer缓存图形,以便更快地滚动。不幸的是,每次我尝试滚动视图时,代码都会崩溃

只要在iOS模拟器中单击并拖动,它就会崩溃,并出现以下错误:

2012-08-04 19:11:09.687 scrolllayertest[3197:c07]-[\uu\NSCFType animationKeys]:发送到实例0x682aa20的无法识别的选择器 2012-08-04 19:11:09.689 scrolllayertest[3197:c07]*终止应用程序 由于未捕获的异常“NSInvalidArgumentException”,原因: '-[\uu NSCFType animationKeys]:发送到实例的选择器无法识别 0x682aa20' *第一次抛出调用堆栈:(0x14b2022 0xeb2cd6 0x14b3cbd 0x1418ed0 0x1418cb2 0x4ca94 0x492a5 0x49459 0x5e157 0x49765 0x1449f1a 0x1415635 0x1415026 0x4966b 0x49765 0x1449f1a 0x1415635 0x1415026 0x4966b 0x43dbd 0x15ca0 0x139cef5 0x1486195 0x13eaff2 0x13e98da 0x13e8d84 0x13e8c9b 0x139b7d8 0x139b88a 0x15626 0x21ed 0x2155 0x1)终止 调用引发异常

我想不出是什么问题。我将把代码粘贴到下面。这也是iOS 5.1在iPad模拟器上实现的。我还没有在设备上测试它

LayerTest.h

#import <UIKit/UIKit.h>

@interface LayerTest : UIScrollView

@end

关于如何修复的任何建议?

如果您可以将其缩小到导致崩溃的行,这将非常有用,因为我在您的代码中的任何地方都没有看到对错误消息中提到的函数的调用。导致崩溃的行似乎是对CGLayerCreateWithContext的调用。也就是说,如果我注释掉对这个函数的调用,就不会出现崩溃。然而,实际的崩溃发生在主循环的某个地方,而不是我的代码。
#import "LayerTest.h"

@interface LayerTest ()
@property (nonatomic,assign) CGLayerRef layer;
@end

@implementation LayerTest
@synthesize layer;

- (void)setLayer:(CGLayerRef)aLayer {
    if (layer)
        CGLayerRelease(layer);
    layer = aLayer;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    self.layer = nil;
}

- (void)awakeFromNib {
    CGSize sz = self.bounds.size;
    sz.width *= 2;
    self.contentSize = sz;
    self.layer = nil;
}

- (void)setContentOffset:(CGPoint)contentOffset {
    [super setContentOffset:contentOffset];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    if (self.layer == nil) {
        self.layer = CGLayerCreateWithContext(context, self.contentSize, NULL);
        CGContextRef contextLayer = CGLayerGetContext(self.layer);
        CGRect contentRect = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
        CGContextSetFillColorWithColor(contextLayer, [[UIColor blackColor] CGColor]);
        CGContextFillRect(contextLayer, contentRect);
        CGContextSetFillColorWithColor(contextLayer, [[UIColor yellowColor] CGColor]);
        CGContextFillEllipseInRect(contextLayer, contentRect);
    }
    if (self.layer)
        CGContextDrawLayerAtPoint(context, CGPointZero, self.layer);
}

@end