objective-c – 停止绘制CATiledLayer

前端之家收集整理的这篇文章主要介绍了objective-c – 停止绘制CATiledLayer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以停止CATiledLayer绘制(drawLayer:inContext)?
它异步绘制,当我尝试释放CATiledLayer使用的CGPDFDocumentRef时,应用程序崩溃(EXC_BAD_ACCESS).

这是我的看法

  1. @implementation TiledPDFView
  2.  
  3. - (id)initWithFrame:(CGRect)frame andScale:(CGFloat)scale{
  4. if ((self = [super initWithFrame:frame])) {
  5.  
  6. CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
  7. tiledLayer.levelsOfDetail = 4;
  8. tiledLayer.levelsOfDetailBias = 4;
  9. tiledLayer.tileSize = CGSizeMake(512.0,512.0);
  10. myScale = scale;
  11. }
  12. return self;
  13. }
  14.  
  15. // Set the layer's class to be CATiledLayer.
  16. + (Class)layerClass {
  17. return [CATiledLayer class];
  18. }
  19.  
  20. - (void)stopDrawing{
  21. CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
  22. [tiledLayer removeFromSuperlayer];
  23. tiledLayer.delegate = nil;
  24. }
  25. // Set the CGPDFPageRef for the view.
  26. - (void)setPage:(CGPDFPageRef)newPage
  27. {
  28. CGPDFPageRelease(self->pdfPage);
  29. self->pdfPage = CGPDFPageRetain(newPage);
  30.  
  31. //self->pdfPage = newPage;
  32. }
  33.  
  34.  
  35. -(void)drawRect:(CGRect)r
  36. {
  37. }
  38.  
  39.  
  40. // Draw the CGPDFPageRef into the layer at the correct scale.
  41. -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
  42. {
  43.  
  44. // First fill the background with white.
  45. CGContextSetRGBFillColor(context,1.0,1.0);
  46. CGContextFillRect(context,self.bounds);
  47.  
  48. CGContextSaveGState(context);
  49. // Flip the context so that the PDF page is rendered
  50. // right side up.
  51. CGContextTranslateCTM(context,0.0,self.bounds.size.height);
  52. CGContextScaleCTM(context,-1.0);
  53.  
  54. // Scale the context so that the PDF page is rendered
  55. // at the correct size for the zoom level.
  56. CGContextScaleCTM(context,myScale,myScale);
  57. CGContextDrawPDFPage(context,pdfPage);
  58. CGContextRestoreGState(context);
  59.  
  60. }
  61.  
  62. // Clean up.
  63. - (void)dealloc {
  64. CGPDFPageRelease(pdfPage);
  65.  
  66. [super dealloc];
  67. }

这是我试图在视图控制器中停止和发布PDF的地方:
v是TiledPDFView的实例

  1. -(void) stopDwaring {
  2. [v stopDrawing];
  3. [v removeFromSuperview];
  4. [v release];
  5. [self.view removeFromSuperview];
  6. self.view = nil;
  7. CGPDFDocumentRelease(pdf);
  8.  
  9. }

解决方法

这篇文章帮助我解决了我自己的麻烦与CATiledLayer.我以Apple的文档中的TiledPDFview.m为例.
由于我需要在某个时候重绘整个视图和所有的图块,所以我使用CATiledLayer作为属性.
退出并取消分配视图控制器时,它使用[CATiledLayer retain]:发送到释放的实例的消息崩溃.
这是我的dealloc方法的视图控制器:
  1. - (void)dealloc {
  2. self.tiledLayer.contents=nil;
  3. self.tiledLayer.delegate=nil;
  4. [self.tiledLayer removeFromSuperlayer];
  5.  
  6. // note: releasing the layer still crashes-
  7. // I guess removeFromSuperlayer releases it already,// but couldn't find documentation so far.
  8. // So that's why it's commented out:
  9. // [self.tiledLayer release],self.tiledLayer=nil;
  10.  
  11. //release the other viewcontroller stuff...
  12. [super dealloc];
  13. }

这对我行得通.希望它有助于某人.

猜你在找的C&C++相关文章