我有一个必须显示一些信息的UIView。由于此视图非常复杂且绘图需要大量时间,因此我将其移至后台线程以避免阻塞主线程。绘图完成后,我将调度回主线程以设置ImageView。
以下代码为我绘制了图纸,但我偶尔会遇到一些崩溃。通常xcode指向[view release];
并说Thread 6: Program received signal: EXC_BAD_ACCESS
NSManagedObjectID *objectID = [self.managedObject objectID];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue,^{
NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] init];
[backgroundContext setPersistentStoreCoordinator:[[self.managedObject managedObjectContext] persistentStoreCoordinator]];
NSManagedObject *mo = [backgroundContext objectWithID:objectID];
CGImageRef resultImage;
CGContextRef context;
void *bitmapData;
CGRect frame = self.frame;
frame.origin = CGPointZero;
CGColorSpaceRef colorSpace;
CGSize canvasSize;
int bitmapByteCount;
int bitmapBytesPerRow;
canvasSize = frame.size;
CGFloat scale = [UIScreen instancesRespondToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0f;
canvasSize.width *= scale;
canvasSize.height *= scale;
bitmapBytesPerRow = (canvasSize.width * 4);
bitmapByteCount = (bitmapBytesPerRow * canvasSize.height);
//Create the color space
colorSpace = CGColorSpaceCreateDeviceRGB();
bitmapData = malloc( bitmapByteCount );
//Check the the buffer is alloc'd
if( bitmapData == NULL ){
DLog(@"Buffer could not be alloc'd");
}
//Create the context
context = CGBitmapContextCreate(bitmapData, canvasSize.width, canvasSize.height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClearRect(context, CGRectMake(0, 0, canvasSize.width, canvasSize.height));
// Setup transformation
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextTranslateCTM(context, 0.0f, canvasSize.height);
CGContextScaleCTM(context, scale, -scale);
UIView *view = [[UIView alloc] initWithFrame:frame];
[view setBackgroundColor:[UIColor clearColor]];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(137, 5, 255, 30)];
[nameLabel setFont:[UIFont fontWithName:@"Arial" size:22.0f]];
[nameLabel setText:[mo valueForKey:@"name"]];
[nameLabel setTextAlignment:UITextAlignmentRight];
[view addSubview:nameLabel];
[nameLabel release];
nameLabel = nil;
... Creating 20 or so label/imageviews all use 'mo' for data access to the NSManagedObject.
[view.layer renderInContext:context];
[view release];
view = nil;
//Get the result image
resultImage = CGBitmapContextCreateImage(context);
UIImage *viewImage = [UIImage imageWithCGImage:resultImage scale:0.0 orientation:UIImageOrientationUp];
dispatch_async(dispatch_get_main_queue(),^ {
[dataImageView setImage:viewImage];
});
//Cleanup
free(bitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(resultImage);
CGContextRelease(context);
});
希望你们能帮助我,因为我无法做到这一点。我试图使用NSZombieEnabled YES运行它,这有时会导致控制台-[UILabel hash]: message sent to deallocated instance 0x22841c00
中出现以下错误
答案 0 :(得分:0)
尝试调查此问题的可能来源: 当你创建UILabel时,你将它们添加到UIView然后你释放它们(没关系)但你也“nil”它们。所以在这一点上你有UILabel由UIView保留,但同时它指向零! 所以最后当你发布“view”时,它会尝试释放它的子视图,并且一旦遇到保留count = 1的UILabel但它已经为nil'd它会得到“deallocated instance”异常。 这可以说明为什么调试器在[查看发布]停止以及为什么NSZombieEnabled抱怨尝试解除分配UILabel:它将其视为解除分配,因为指针指向nil。 所以可能的解决方案:删除“nameLabel = nil”语句。