LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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 {

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!


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)


Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free 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.