开发者

EXC_BAD_ACCESS in drawRect

开发者 https://www.devze.com 2022-12-30 08:40 出处:网络
The code below \"sometimes\" causes a crash (EXC_BAD_ACCESS) when run on the device. Never on the simulator.

The code below "sometimes" causes a crash (EXC_BAD_ACCESS) when run on the device. Never on the simulator.

To reproduce it I keep 开发者_JAVA技巧overlaying a modal view controller over my table view controller. It usually happens when the modal view controller is dismissed.

Any ideas why this happens?

CGContextRef context = UIGraphicsGetCurrentContext();

//set the background of the cell
[self.backgroundColor set];
CGContextFillRect(context, rect);

// get cached image
UIImage *image = [[ImageUtil sharedInstance] getImageByRouteType:route.type];
CGSize imageSize = CGSizeMake(IMAGE_WIDTH, IMAGE_WIDTH);
// DEBUGGER STOPS ON THIS NEXT LINE, image object is fine though
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];

[...]

Thanks


If you use drawInRect in a multiple tread situation like NSOperationQueue, try use a lock to avoid "drawInRect" is called in more than one thread. I met a similar issue and solved it in this way.

@synchronized([UIImage class]){
    UIGraphicsBeginImageContext(newSize);
    CGRect rect = CGRectMake(0.0, 0.0, newSize.width, newSize.height);
    [self drawInRect: rect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号