jjzjj

objective-c - 绘图层 :inContext - Unrecognized selector sent to instance

coder 2023-09-25 原文

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

-[__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 的委托(delegate)是self

这就是我在主 Controller 上的调用方式:

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

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

我这样做的方式不对吗?我不明白为什么在我的主 Controller 上绘制图层时效果很好,如果在 PDFViewController 上进行绘制会导致错误。方法在那里,委托(delegate)是self。那么为什么选择器会失败呢?

最佳答案

[转自评论,原来是答案!]

这听起来像是一个内存管理问题。你试过 NSZombieEnabled 了吗?您是否有可能过度发布某些内容?你在使用 ARC 吗?

关于objective-c - 绘图层 :inContext - Unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560425/

有关objective-c - 绘图层 :inContext - Unrecognized selector sent to instance的更多相关文章

随机推荐