Thursday, October 8, 2026
HomeiOS Developmentios - Async/await downside when attempting to replace UIImageView

ios – Async/await downside when attempting to replace UIImageView

[ad_1]

I am attempting to get all frames of a video and show them in a UIImageView once more. (In the midst of this there may be an AI which is decoding these Photographs however that is not related for this query)

So to start with the person can choose a video of the library and I will begin a activity which calls my methodology:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo information: [UIImagePickerController.InfoKey : Any]){
    imagePickerController.dismiss(animated: true, completion: nil)
    Job{
        guard let movieUrl = information[.mediaURL] as? URL else { return }
        await previewImageFromVideo(url: movieUrl)
    }
}

Now my Downside is within the following code:
Why does this solely work if i am explicitly open a brand new activity in each loop and await it with .worth?

func previewImageFromVideo(url:URL) async{
    let asset = AVAsset(url:url as URL)
    let videoDuration = asset.length
    
    let generator = AVAssetImageGenerator(asset: asset)
    generator.requestedTimeToleranceAfter = CMTime.zero
    generator.requestedTimeToleranceBefore = CMTime.zero
    
    let tracks = asset.tracks(withMediaType: .video)
    let fps = tracks.first?.nominalFrameRate
    let totalTimeLength = Int(videoDuration.seconds * Double(fps!))
    
    for i in 0 ..< totalTimeLength {
        let cmTime = CMTimeMake(worth: Int64(i), timescale: Int32(fps!))
        await Job{
            var picture:CGImage
            do{
                picture = attempt generator.copyCGImage(at:cmTime, actualTime:nil)
                self.currentFrame = picture
                self.poseNet.predict(picture)
            }catch{
                
            }
        }.worth
        
    }
}

The remainder of the code is right here, the predict methodology:

func predict(_ picture: CGImage) {
    DispatchQueue.international(qos: .userInitiated).async {
        // Wrap the picture in an occasion of PoseNetInput to have it resized
        // earlier than being handed to the PoseNet mannequin.
        let enter = PoseNetInput(picture: picture, dimension: self.modelInputSize)

        guard let prediction = attempt? self.poseNetMLModel.prediction(from: enter) else {
            return
        }

        let poseNetOutput = PoseNetOutput(prediction: prediction,
                                          modelInputSize: self.modelInputSize,
                                          modelOutputStride: self.outputStride)

        DispatchQueue.most important.async {
            self.delegate?.poseNet(self, didPredict: poseNetOutput)
        }
    }

and the delegate which is setting the picture:

    func poseNet(_ poseNet: PoseNet, didPredict predictions: PoseNetOutput) {
    defer {
        // Launch `currentFrame` when exiting this methodology.
        self.currentFrame = nil
    }
    
    guard let currentFrame = currentFrame else {
        return
    }
    
    let poseBuilder = PoseBuilder(output: predictions,
                                  configuration: poseBuilderConfiguration,
                                  inputImage: currentFrame)
    
    var poses = poseBuilder.poses
    
    
    self.previewImageView.present(poses: poses, on: currentFrame)
}

It might probably’t think about why the additional duties are essential, as a result of I already began a activity within the delegate of the UIImagePicker. If I dont add the additional duties within the loop, the entire UI is caught and I solely see the final picture on the finish.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments