UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 58: Animation not working when UIImagePickerController sourceType is camera

Forums > 100 Days of Swift

So I've been stuck at the 2nd challenge in UIImagePickerController, i.e., doing a Fade in when an image is imported in InstaFilter app.

I have these two functions,

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage else { return }
        imageView.alpha = 0
        dismiss(animated: true)

        currentImage = image
        print("S : \(Date())")

        setupForProcessing()
    }

    func setupForProcessing() {
        let beginImage = CIImage(image: currentImage)
        currentFilter.setValue(beginImage, forKey: kCIInputImageKey)

        applyProcessing()

        UIView.animate(withDuration: 1 , delay: 0, options: [.layoutSubviews], animations: {
            self.imageView.alpha = 1
//            self.imageView.layoutIfNeeded()
        }) { fin in
            print("F : \(Date())")
        }
    }

And I added the two prints just to see who much time elasped between setting the alpha to 0 and then back to 1.

This is working fine when I import an image from Photo Library, but not when clicking a picture through camera. Case if point, here is the output when importing image with sourceType == .photoLibrary

S : 2021-08-01 05:11:36 +0000
F : 2021-08-01 05:11:37 +0000

And here's the output with .camera

S : 2021-08-01 05:11:23 +0000
F : 2021-08-01 05:11:23 +0000

I am at my wit's end, and either my google-fu is weaker than I think, or there just isn't much out there on this problem. Any help on this is greatly appreciated!

You can find the repo for this project here: https://github.com/AtinAgnihotri/FilterIt

3      

It seems the dismissal of UIImagePicker was disrupting the animations. A simple solve then was to put the code to happen after the dismissal in dismiss method's completion block, like so:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage else { return }
        imageView.alpha = 0
        print("Reached Here")
        dismiss(animated: true) { [weak self] in
            self?.currentImage = image
            self?.setupForProcessing()
        }
    }

Hope this helps someone going forward!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.