drawLayer:inContext - 发送到实例的无法识别的选择器

时间:2011-07-03 00:46:51

标签: objective-c ios cocoa-touch

我正在尝试创建一个绘制图层的UIViewController,如果这个UIViewController是主要的,它可以正常工作。但是,如果我尝试在另一个控制器内初始化它,然后将其视图添加为主控制器视图的子视图,则会导致以下错误:

-[__NSCFType drawLayer:inContext:]: unrecognized selector sent to instance 0x155140

以下是我的自定义UIViewController(PDFPageViewController)的相关代码:

- (void)loadDocument:(PDFDocument *)document
{
    self._document = document;

    CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(self._document.page, kCGPDFCropBox));

    pageRect.origin.x = (self.view.frame.size.width / 2) - (pageRect.size.width / 2) - 35;

    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
    tiledLayer.levelsOfDetail = 1000;
    tiledLayer.levelsOfDetailBias = 1000;
    tiledLayer.frame = pageRect;

    contentView = [[UIView alloc] initWithFrame:pageRect];
    [contentView.layer addSublayer:tiledLayer];

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 1000;
    [scrollView addSubview:contentView];

    [self.view addSubview:scrollView];   

    NSLog(@"%@", self); // Just checking if there's nothing overwriting the layer's delegate
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return contentView;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    if(self._document) {
        CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
        CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(self._document.page, kCGPDFCropBox, layer.bounds, 0, true));
        CGContextDrawPDFPage(ctx, self._document.page);
    }
}

drawLayer 方法在那里, CALayer 的委托是 self

这就是我在主控制器上调用它的方式:

pageViewController = [[[PDFPageViewController alloc] initWithNibName:NULL bundle:NULL] autorelease];
[pageViewController loadDocument:self.document];

[self.view addSubview:[pageViewController view]];

我不是这样做的正确方法吗?我无法理解为什么我在主控制器上绘制图层时效果正常,如果在PDFViewController上进行绘制,则会导致错误。方法就在那里,委托是 self 。那么为什么选择器失败?

1 个答案:

答案 0 :(得分:4)

[从评论中重新发布,因为它原来是答案!]

这听起来像是一个内存管理问题。你试过NSZombieEnabled吗?你有没有机会过度发布东西?你在使用ARC吗?