ios - ios7 : CGContextDrawImage or UIView setImage? 速度大幅下降

标签 ios objective-c ios7 cgcontext

好的,我的 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/

相关文章:

ios - 如何在后台更新 MPNowPlayingInfoCenter 以及 AVQueuePlayer 中的轨道发生变化

ios - 应用程序传输安全问题

ios - 在 WatchKitExtension 中混合 swift 和 objective-c

ios - 无法访问保存在 Xamarin.iOS 钥匙串(keychain)值中

objective-c - NSDate 的字符串描述

ios - 另一双眼睛关注我的约会策略?

ios - UITableView 底部不需要的黑色单元格

ios - 如何在“照片”图像的“打开方式或共享方式”中添加应用程序?

Objective-C NSPredicate 链

ios - 将 translateAutoResizingMaskIntoConstraints 设置为 NO 时不显示 UIView