CGImage
转换为UIImage
并将其添加到主线程上的UIImageView
。UICollectionViewCell
上,从alpha值0到1。UICollectionView
一开始,这种行为正常,但很快就会陷入不可预测的状态,并且通常会很快导致EXC_BAD_ACCESS
错误,在将uiimage转换为cgimage的过程中发生错误,反之亦然。
//setup for background drawing
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef context;
context = CGBitmapContextCreate(NULL, 250, backgroundHeight - 112, 8, 250 * 4, colorSpaceRef, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colorSpaceRef);
//Take the image I want drawn, convert to cgimage for background drawing
CGImageRef image = contentImage.CGImage;
CGContextDrawImage(context, CGRectMake(0, 0, 250, backgroundHeight - 112), image);
CGImageRelease(image);
CGImageRef outputImage = CGBitmapContextCreateImage(context);
imageRef = outputImage;
[self performSelectorOnMainThread:@selector(addImageToView) withObject:nil waitUntilDone:YES];
CGContextRelease(context);
CGImageRelease(outputImage);
addImageToView方法只是创建一个图像并添加到我的UIImageView
UIImage * image = [UIImage imageWithCGImage:imageRef];
[photoView setImage:image];
在cellForRowAtIndexPath方法中调用这些方法,以及我的方法fadeInImage
[UIView animateWithDuration:.6 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
photoView.alpha = 1;
}completion:nil];
当我跑步时,我收到Bad Access电话并崩溃。我猜它与主线程和后台线程之间的图像传递有关。有任何想法吗?谢谢你们。
答案 0 :(得分:1)
我认为,只要您没有使用CGImageCreate
或使用CGImageRetain
创建图片,就不必使用CGImageRelease(image);
。一旦停止使用它,它应该会自动释放。看看吧。
答案 1 :(得分:0)
不要发布第一张图片CGImageRelease(image);
最好改变一下:
CGImageRef outputImage = CGBitmapContextCreateImage(context);
imageRef = outputImage;
[self performSelectorOnMainThread:@selector(addImageToView) withObject:nil waitUntilDone:YES];
要:
CGImageRef outputImage = CGBitmapContextCreateImage(context);
UIImage* result = [UIImage imageWithCGImage:imageRef]
[self performSelectorOnMainThread:@selector(addImageToView:) withObject:result waitUntilDone:YES];
然后:
- (void) addImageToView:(UIImage*) image {
[photoView setImage:image];
}
甚至:
[photoView performSelectorOnMainThread:@selector(setImage:) withObject:result waitUntilDone:YES];