ios5: drawRect called twice in CATiledLayer for the same Rect

假如想象 提交于 2019-12-23 03:03:54

问题


I have a problem with a CATiledLayer. It is working perfect in on iOS 4, but has problems on iOS 5.

My Problem ist that drawRect is called twice for the same rect from two different threads at the same time. Since I load images in this call, it causes the view to load very slow.

2011-10-18 14:07:18.802 APP[12436:19003] drawRect:{{0, 400}, {368, 400}} (view:<TiledScrollColumn: 0x91bc880; frame = (0 1600; 368 5400); userInteractionEnabled = NO; layer = <CATiledLayer: 0x919c1b0>>)
2011-10-18 14:07:18.805 APP[12436:1b103] drawRect:{{0, 400}, {368, 400}} (view:<TiledScrollColumn: 0x91bc880; frame = (0 1600; 368 5400); userInteractionEnabled = NO; layer = <CATiledLayer: 0x919c1b0>>)

Has anyone an idea what could cause that or what I could do to fix that? I'm not doing special stuff with the view, it's based on the photoscroller example.

Bastian


回答1:


I found a workaround. Create a serial (only one operation at at time) dispatch queue and perform all your drawing inside it, then cache the result.

I've attached the code I'm using, note I'm using a subclass of CATiledLayer, rather than drawing it through a delegate or other techniques.

I'm also creating an indefinite tile cache, which may cause memory issues depending on your situation. You may need to manage the number of tiles in the cache, deleting old ones as new ones are added. The cache should also be cleared when a low memory warning is received. If anyone adds these improvements, please feel free to edit my answer to include it.

- (id)init
{
  if (!(self = [super init]))
    return nil;

  tileCache = [[NSMutableDictionary alloc] init];

  return self;
}

- (void)drawInContext:(CGContextRef)layerContext
{
  // CATiledLayer has a bug, where it spawns multiple threads for drawing, and then tries to draw the same tile multiple times simultaniously on separate threads
  // so we create our own serial background queue, and do dispatch_async on it. This will cache each draw operation, so multiple calls on one tile are efficient
  static dispatch_queue_t drawQueue = NULL;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
      drawQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
  });

  dispatch_sync(drawQueue, ^{

    // no map ways? draw nothing
    if (!self.mapWays)
      return;

    // load from cache?
    CGRect tileRect = CGContextGetClipBoundingBox(layerContext);
    NSString *tileCacheKey = [NSString stringWithFormat:@"%f%f%f%f", tileRect.origin.x, tileRect.origin.y, tileRect.size.width, tileRect.size.height];
    __block UIImage *tileImage;
    dispatch_sync(dispatch_get_main_queue(), ^{
      tileImage = [tileCache objectForKey:tileCacheKey];
    });
    if (tileImage) {
      CGContextDrawImage(layerContext, tileRect, tileImage.CGImage);
      return;
    }

    // prepare to draw the tile image
    UIGraphicsBeginImageContextWithOptions(tileRect.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // filp coords
    CGContextTranslateCTM(context, 0, tileRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);


    /*** do actual drawing here ***/


    // store tile in cache
    tileImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    dispatch_sync(dispatch_get_main_queue(), ^{
      [tileCache setObject:tileImage forKey:tileCacheKey];
    });

    // draw the tile
    CGContextDrawImage(layerContext, tileRect, tileImage.CGImage);
  });
}


来源:https://stackoverflow.com/questions/7807193/ios5-drawrect-called-twice-in-catiledlayer-for-the-same-rect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!