是否可以停止CATiledLayer绘制(drawLayer:inContext)?
它异步绘制,当我尝试释放CATiledLayer使用的CGPDFDocumentRef时,应用程序崩溃(EXC_BAD_ACCESS).
它异步绘制,当我尝试释放CATiledLayer使用的CGPDFDocumentRef时,应用程序崩溃(EXC_BAD_ACCESS).
这是我的看法
@implementation TiledPDFView - (id)initWithFrame:(CGRect)frame andScale:(CGFloat)scale{ if ((self = [super initWithFrame:frame])) { CATiledLayer *tiledLayer = (CATiledLayer *)[self layer]; tiledLayer.levelsOfDetail = 4; tiledLayer.levelsOfDetailBias = 4; tiledLayer.tileSize = CGSizeMake(512.0,512.0); myScale = scale; } return self; } // Set the layer's class to be CATiledLayer. + (Class)layerClass { return [CATiledLayer class]; } - (void)stopDrawing{ CATiledLayer *tiledLayer = (CATiledLayer *)[self layer]; [tiledLayer removeFromSuperlayer]; tiledLayer.delegate = nil; } // Set the CGPDFPageRef for the view. - (void)setPage:(CGPDFPageRef)newPage { CGPDFPageRelease(self->pdfPage); self->pdfPage = CGPDFPageRetain(newPage); //self->pdfPage = newPage; } -(void)drawRect:(CGRect)r { } // Draw the CGPDFPageRef into the layer at the correct scale. -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context { // First fill the background with white. CGContextSetRGBFillColor(context,1.0,1.0); CGContextFillRect(context,self.bounds); CGContextSaveGState(context); // Flip the context so that the PDF page is rendered // right side up. CGContextTranslateCTM(context,0.0,self.bounds.size.height); CGContextScaleCTM(context,-1.0); // Scale the context so that the PDF page is rendered // at the correct size for the zoom level. CGContextScaleCTM(context,myScale,myScale); CGContextDrawPDFPage(context,pdfPage); CGContextRestoreGState(context); } // Clean up. - (void)dealloc { CGPDFPageRelease(pdfPage); [super dealloc]; }
这是我试图在视图控制器中停止和发布PDF的地方:
v是TiledPDFView的实例
-(void) stopDwaring { [v stopDrawing]; [v removeFromSuperview]; [v release]; [self.view removeFromSuperview]; self.view = nil; CGPDFDocumentRelease(pdf); }
解决方法
这篇文章帮助我解决了我自己的麻烦与CATiledLayer.我以Apple的文档中的TiledPDFview.m为例.
由于我需要在某个时候重绘整个视图和所有的图块,所以我使用CATiledLayer作为属性.
当退出并取消分配视图控制器时,它使用[CATiledLayer retain]:发送到释放的实例的消息崩溃.
这是我的dealloc方法的视图控制器:
由于我需要在某个时候重绘整个视图和所有的图块,所以我使用CATiledLayer作为属性.
当退出并取消分配视图控制器时,它使用[CATiledLayer retain]:发送到释放的实例的消息崩溃.
这是我的dealloc方法的视图控制器:
- (void)dealloc { self.tiledLayer.contents=nil; self.tiledLayer.delegate=nil; [self.tiledLayer removeFromSuperlayer]; // note: releasing the layer still crashes- // I guess removeFromSuperlayer releases it already,// but couldn't find documentation so far. // So that's why it's commented out: // [self.tiledLayer release],self.tiledLayer=nil; //release the other viewcontroller stuff... [super dealloc]; }
这对我行得通.希望它有助于某人.