ios - 在大量计算期间如何使屏幕停止卡住?

标签 ios swift

好的,我要做的是更改特定图像的像素数据。我想使用每次作为进度条循环时向右移动的 UIView。然而,正在发生的事情是屏幕在计算过程中卡住,并且在完成之前没有任何反应。状态栏时钟也不会更新。有没有办法以某种方式将这些计算移到后台并仍然使用屏幕空间?

func lock() {

    let lockView =  UIImageView(image: self.photoBackgroundView.image!)
    var progressBar = UIView(frame: CGRectMake(-self.view.width,0,self.view.width,20)
    let increment = progressBar.width/self.photoBackgroundView.width
    var password = self.textField.text!
    if password == "" {
        password = " "
    }
    var currentIndex = 0



    let imageRect = CGRectMake(0, 0, self.photoBackgroundView.image!.size.width,self.photoBackgroundView.image!.size.height)

    UIGraphicsBeginImageContext(self.photoBackgroundView.image!.size)
    let context = UIGraphicsGetCurrentContext()

    CGContextSaveGState(context)
    CGContextDrawImage(context, imageRect, self.photoBackgroundView.image!.CGImage)
    for x in 0...Int(self.photoBackgroundView.image!.size.width) {
        print(x)

        progressBar.frame.origin.x += (CGFloat(x) * increment)
        self.view.addSubView(progressBar)


        for y in 0...Int(self.photoBackgroundView.image!.size.height) {
            let pointColor = lockView.layer.colorOfPoint(CGPoint(x: x, y: y))

            if currentIndex == Array(password.characters).count {
                currentIndex = 0
            }
            let red = encrypt(pointColor.components.red, passwordChar: Array(password.characters)[currentIndex], currentIndex: currentIndex, x: x, y: y)
            currentIndex++
            if currentIndex == Array(password.characters).count {
                currentIndex = 0
            }
            let green = encrypt(pointColor.components.green, passwordChar: Array(password.characters)[currentIndex], currentIndex: currentIndex, x: x, y: y)
            currentIndex++
            if currentIndex == Array(password.characters).count {
                currentIndex = 0
            }
            let blue = encrypt(pointColor.components.blue, passwordChar: Array(password.characters)[currentIndex], currentIndex: currentIndex, x: x, y: y)
            currentIndex++

            CGContextSetRGBFillColor(context, red, green, blue, pointColor.components.alpha)
            CGContextFillRect(context, CGRectMake(CGFloat(x), CGFloat(y), 1, 1))

        }



    }

    CGContextRestoreGState(context)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    self.photoBackgroundView.image = newImage
    self.slideView.addSubview(photoBackgroundView)
    self.view.addSubview(self.slideView)
}

最佳答案

您可以在 background thread 中进行这些计算.

swift 2:

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
    // Put the calculations here
    dispatch_async(dispatch_get_main_queue()) {
        // any updates of the UI need to be here which will be executed in the main thread after your background task completes. For example adding subviews
    }
}

swift 3:

参见 this answer

关于ios - 在大量计算期间如何使屏幕停止卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34547251/

相关文章:

ios - 从包含 CGImage 的 UIImage 对象获取 NSData

iOS - 约束在 viewdidload 中以意想不到的方式动画?

ios - 打开方向功能不以编程方式打开苹果 map

ios - 如何在 NSURLSession(Background Session) 事件的情况下测试 Background App Launch?

ios - 核心图像检测器(CIDetector)未检测到 QRCodes

ios - NSPredicate 不包括 Loop 和 Bounce Live Photos

ios - Swift Stepper Action 更改同一单元格内的 UITextField 和 UILabel

ios - 双返回到上一个 View - Swift

ios - AVAssetExportSession、可变格式配置文件、CABAC 和重构

ios - UIImage 在将其转换为 CIImage、然后转换为 CGImage 并返回 UIImage 时失去其方向