0

I've built a renderer that manipulates CIImages and I'd like to show the progress. Tho I can't seem to figure out how update the ui. The ui updates when the render is done.

This is my setup, simplified:

DispatchQueue.global(qos: .background).async(execute: {
    for i in 0..<count {
        // CIImage manipulation and definition of progress
        DispatchQueue.main.async(execute: {
            self.progress.doubleValue = progress
        })
    }
})

Got the DispatchQueue code from here.

Note, I'm getting the spinning wheel mouse.

Here's my render method in full detail:

@IBAction func render_now(_ sender: Any) {

    self.render_button.isEnabled = false

    self.duration = self.player_x1?.currentItem?.asset.duration.seconds

    let videoATrack: AVAssetTrack = (self.player_x1?.currentItem?.asset.tracks(withMediaType: AVMediaTypeVideo).last)!
    self.fps = CMTimeScale(videoATrack.nominalFrameRate)
    self.frame_count = Int(self.duration! * Double(self.fps!))

    let asset_generator = AVAssetImageGenerator(asset: (self.player_x1?.currentItem?.asset)!)
    var time_zero = kCMTimeZero
    var asset_image_arr: [CGImage] = []
    do {
        for i in 0..<self.frame_count! {

            let time = Double(i) / Double(self.fps!)
            let asset_image = try asset_generator.copyCGImage(at: CMTime(seconds: time, preferredTimescale: self.fps!), actualTime: &time_zero)
            asset_image_arr.append(asset_image)

        }
    } catch {print("err")}

    let image_size = CGSize(width: asset_image_arr[0].width, height: asset_image_arr[0].height)
    var ci_image_arr: [CIImage] = []
    for i in 0..<self.frame_count! {
        ci_image_arr.append(CIImage(cgImage: asset_image_arr[i], options: nil))
    }

    let x_factor = Int(NSDecimalNumber(decimal: pow(2, self.radio_index! + 1)))

    let filter_brightness = CIFilter(name: "CIToneCurve")
    let brightness = 1.0 / CGFloat(x_factor)
    filter_brightness?.setValue(CIVector(x:0.0, y: 0.0), forKey: "inputPoint0")
    filter_brightness?.setValue(CIVector(x:0.25, y: 0.25 * brightness), forKey: "inputPoint1")
    filter_brightness?.setValue(CIVector(x:0.50, y: 0.5 * brightness), forKey: "inputPoint2")
    filter_brightness?.setValue(CIVector(x:0.75, y: 0.75 * brightness), forKey: "inputPoint3")
    filter_brightness?.setValue(CIVector(x:1.0, y: 1.0 * brightness), forKey: "inputPoint4")

    let filter_add = CIFilter(name: "CIAdditionCompositing")

    var images: [NSImage] = []

    DispatchQueue.global(qos: .background).async(execute: {

        for i in 0..<self.frame_count! / x_factor {

            filter_brightness?.setValue(ci_image_arr[i * x_factor], forKey: kCIInputImageKey)
            var ci_image: CIImage = (filter_brightness?.outputImage)!

            for x in 1..<x_factor {

                filter_brightness?.setValue(ci_image_arr[i * x_factor + x], forKey: kCIInputImageKey)
                let ci_image_add: CIImage = (filter_brightness?.outputImage)!

                filter_add?.setValue(ci_image, forKey: kCIInputImageKey)
                filter_add?.setValue(ci_image_add, forKey: kCIInputBackgroundImageKey)

                ci_image = (filter_add?.outputImage)!

            }
            let rep: NSCIImageRep = NSCIImageRep(ciImage: ci_image)
            let nsImage: NSImage = NSImage(size: rep.size)
            nsImage.addRepresentation(rep)

            images.append(nsImage)

            let progress = Double(i) / Double(self.frame_count! / x_factor - 1) * 100.0
            DispatchQueue.main.async(execute: {
                print(progress)
                self.progress.doubleValue = progress
            })

        }

    })

    let desktopURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!

    let movie_out = MovieOut()
    movie_out.writeImagesAsMovie(images, path: self.movie_out_x_urls[self.radio_index! - 1].path, size: image_size, fps: self.fps!) { () -> () in

        let player_x = AVPlayer(url: self.movie_out_x_urls[self.radio_index! - 1])
        self.player_view.player = player_x
        self.view.addSubview(self.player_view)
        self.save_button.isEnabled = true
    }

}

Dose anyone know what I'm doing wrong?

Community
  • 1
  • 1
Heestand XYZ
  • 1,993
  • 3
  • 19
  • 40
  • Show more code, please. – matt May 15 '17 at 20:17
  • Did you set `minValue` and `maxValue` for your progress indicator? Is the value of `progress` properly falling within those min and max values? E.g., progress indicators range from 0 to 100 in macOS (not 0.0 and 1.0, as you might expect if you came from iOS world). – Rob May 15 '17 at 22:00
  • See if your code gets called by adding a breakpoint / or by adding print statements – user1046037 May 15 '17 at 23:54
  • The code get's called and the ui updates, tho after the for loop is done. I'll add some more code. – Heestand XYZ May 16 '17 at 09:40
  • all print(progress) is not printed until the end... – Heestand XYZ May 16 '17 at 10:06

0 Answers0