WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

1      

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!

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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.