TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.