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

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)
    }
}

2      

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

2      

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

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

2      

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

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!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.