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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.