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

Correct way to load a Raw image into an Image view

Forums > Swift

I am trying to load a raw image from a Canon mirrorless camera (these are files with a .CR3 extension) to view in a Swift UI app. It appears that you use the contents of one of these files to create a CIFilter object. However all of the relevant initializers for this object seeem to be deprecated (see here: https://developer.apple.com/documentation/coreimage/cifilter). Is anyone aware of the correct way to load one of these images? This is how I have found to do it currently, however it is using the aforementioned deprecated initializer.

struct ContentView: View {
    @State private var image: Image?
    var body: some View {
        VStack {
            image?
                .resizable()
                .scaledToFit()
        }
        .onAppear(perform: loadImage)
    }
    func loadImage() {
        let path = "/Users/sam/Downloads/8B0A1479.CR3"
        let url = URL(fileURLWithPath: path)
        let data = try! Data(contentsOf: url)
        let filter =  CIFilter(imageData: data)
        let ciImage = (filter?.outputImage)
        let rep = NSCIImageRep(ciImage: ciImage!)
        let nsImage = NSImage(size: rep.size)
        nsImage.addRepresentation(rep)
        image = Image(nsImage: nsImage)
    }
}

   

Hi. I am in a very similar situation. I also want to load and convert binary and UInt16 data arrays into raw image data. Did you find a different way on how to do this?

I was looking at the RawPhoto tutorials from Apple, which is not very transparent to me in this context, but it might be hidden in there somewhere: https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/capturing_photos_in_raw_and_apple_proraw_formats

   

I think we are suppsoed to use this (new?) CIRAWFilter class:

https://developer.apple.com/documentation/coreimage/cirawfilter

   

This works for me with a Canon raw photo (CR2):

let rawimgpath = "/...path.../IMG_4650.CR2"
let rawimgurl = URL(fileURLWithPath: rawimgpath)

let rawfilter = CIRAWFilter(imageURL: rawimgurl)
let rawimg = rawfilter?.outputImage

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

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.