English 中文(简体)
为什么GCD代码在绘图矩形中崩溃?
原标题:Why is GCD code crashing in drawRect?

这是内部绘图矩形 :

NSString *generated = [Entry generateString];

const char *cString = (const char*)[generated UTF8String];

dispatch_queue_t queue = dispatch_queue_create(cString, NULL);*/

dispatch_async(queue, ^{

    Media *media = [self.entry.media objectAtIndex:i];

    UIImage *image = [media getThumbnail];

    dispatch_async(dispatch_get_main_queue(), ^{

        int bottom = [JHomeViewCell yOfMessageBottomWithMessageHeight:self.cellInfo.messageHeight
                                                            withMonth:self.cellInfo.hasMonth];

        CGRect frame = CGRectMake(87 + (68 * i),
                                  bottom,
                                  THUMBNAIL_SIZE.width,
                                  THUMBNAIL_SIZE.height);

        [image drawInRect:frame];
    });

});

现在,它正在崩溃到对象AtIndex 线上。 它不是一个无效的索引。 代码在这里外面工作正常 。

我有一个错误:

Terminating app due to uncaught exception  NSInternalInconsistencyException , reason:  statement is still active 

EDIT2:我输入了NSLogs, 最先得到媒体的. count。 它以同样的错误坠毁 。

问题回答

问题在于您重新画出您应该去的地方。 当您从 < code> drawRect: 返回时, 您告诉操作系统您已经完成了 。 您不能在从 < code> drawRect: 返回之前生成一个无序线条, 并期望您能够通过返回主线条来进行绘图, 因为一旦您从 < code> drawRect: 返回, 上下文将被销毁 。

通常的做法是将信息缓存在一条单独的线索上,然后在您需要绘制的区域调用 invalidateRect: ,允许您的 drawRect: 方法除快速抽出外什么都不做。

如果您想要懒惰地装入缩略图, 我建议将缩略图的复制件存放在私人位置( 也许使用 NSCache 并使用您的图像标识符作为密钥), 然后如果图像存在缓存中, 当 < code> drawRect: 被调用, 绘制图像, 否则绘制占位符, 使用 GCD 将图像添加到缩略图生成的队列中 。 在发送队列呼叫中 :

  • retrieve the thumbnail
  • store it in the cache
  • call invalidateRect on the area that contains the image

如果您发现很难计算位置, 或者您正在非常快地获得缩略图, 您可以延迟无效矩形 : 并且让它以最后发送_ async 方式在您绘制常规的结尾处被调用( 但只有在您排队缩略图用于检索时才如此 ) 。 这将导致一个单一的全区域重划, 而不是每个区域包含一个缩略图的多个重划 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签