好的,我的 ios7 有一个严重的问题。我有一个代码,用户可以在其中拖动一个控件,当他拖动时,界面会更新为经过处理的图像。这在 iOS 6.1 之前非常快,现在非常慢。
我的代码是这样工作的:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//get initial position
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//calculates new position
applyEffects();
- (void)applyEffects {
UIGraphicsBeginImageContext();
//CGContextTranslateCTM, CGContextScaleCTM, CGContextDrawImage and stuff like that
UIImage *blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
preview.image = blendedImage; //preview is a UIImageView
现在的问题是,登录后,我意识到每次我在屏幕上移动手指时都会调用 applyEffects,但 View 更新的次数非常少,我认为这就是它看起来如此缓慢的原因。
所以,关于为什么“preview.image = blendedImage;”的任何想法不再更新 View 了吗?我错过了什么吗?
同样,这只发生在 iOS 7 上。
提前致谢!
最佳答案
将我的 applyEffects 更改为此:
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
if (!running) {
running = true;
dispatch_async(myQueue, ^{
UIGraphicsBeginImageContext();
//CGContextTranslateCTM, CGContextScaleCTM, CGContextDrawImage and stuff like that
UIImage *blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
preview.image = blendedImage;
running = false;
});
});
}
现在它将创建一个单独的线程来应用效果,并在准备就绪时更新 UI。这样我就不会阻塞 UI 触摸事件处理。
if (!running) 是一个愚蠢的控制,让它一次只运行一次。
理想的方法是扔掉所有代码并用 GPUImage 重新实现它,但这将需要更多时间,因此这是一个临时解决方案。
关于ios - ios7 : CGContextDrawImage or UIView setImage? 速度大幅下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18935995/