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

Project 13 - converting flips the image.

Forums > 100 Days of SwiftUI

Hi everyone, I've hit a roadblock in my project 13 when playing around with new topics, when I select the image using the PhotosPicker it shows up just fine:

    func loadImage() {
        Task {
            do {
                guard let imageData = try await selectedItem?.loadTransferable(type: Data.self) else { return }
                guard let inputUiImage = UIImage(data: imageData) else { return }
                inputTestUIImage = inputUiImage
                image = Image(uiImage: inputUiImage) // gets displayed in a view just right
                ciImage = CIImage(image: inputUiImage)
            } catch {
                print(error)
            }
        }
    }

I've stripped down the processing code and just left the conversion, but basically when I run processImage(), it flips an iPhone photo randomly:

    func processImage() {
        ciImage = CIImage(image: inputTestUIImage!)
        guard let cgImage = CIContext().createCGImage(ciImage!, from: ciImage!.extent) else { return }
        let outputUiImage = UIImage(cgImage: cgImage)
        let imageOutput = Image(uiImage: outputUiImage)
        image = imageOutput
    }
}

(Don't mind force unwrapping, it's my testing code.)

You can see it clear when using that flower photo captured on the iPhone X in simulator:

(Click here if the image doesn't show up)

Any thoughts and help would be appreciated!

2      

You can set the orientation for it to be the right way round. UIImage does some weird stuff when processing. Something like this:

       let outputImage = UIImage(cgImage: CGImage, scale: inputImage.scale, orientation: inputImage.imageOrientation)
       image = Image(uiImage: outputImage)

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.