App crashes while trying to save CIImage after applying CIFilter

Hi,


I hope somebody is able to help me.

I am loading 2 DNG files from the apps documents folder using a CIFilter. That is working fine.

After that I combine these images using a CIMaximumCompositing CIFilter.

When I now try to save the outputImage to a JPEG file, my app crashes with the following output. (On line 19)


Message from debugger: Terminated due to memory issue


When I try to save the 2 loaded DNG files without any processing everything is working fine too.


Here is my Code:


    private func process()
    {
        let raw_path_1   : String   = _current_scan._path.path + "/raw/line_0_0.dng"
        let raw_filter_1 : CIFilter = CIFilter( imageURL: URL(fileURLWithPath: raw_path_1), options: nil )
        let raw_image_1  : CIImage  = raw_filter_1.outputImage!
       
        let raw_path_2   : String   = _current_scan._path.path + "/raw/line_1_0.dng"
        let raw_filter_2 : CIFilter = CIFilter( imageURL: URL(fileURLWithPath: raw_path_2), options: nil )
        let raw_image_2  : CIImage  = raw_filter_2.outputImage!
       
        let result_image : CIImage  = raw_image_1.applyingFilter( "CIMaximumCompositing", parameters: [kCIInputBackgroundImageKey: raw_image_2] )
        let result_path  : String  = _current_scan._path.path + "/raw/line_0.jpeg"
       
        if let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)
        {
            do
            {
                let context = CIContext()
                try context.writeJPEGRepresentation( of: result_image, to: URL(fileURLWithPath: result_path), colorSpace: colorSpace, options: [kCGImageDestinationLossyCompressionQuality as CIImageRepresentationOption : 1.0] )
            }
            catch
            {
                print( error.localizedDescription )
            }
        }
    }

Thank you all!

Answered by DTS Engineer in 397741022

Does this still happen if you disable Metal API Validation in your build scheme?

After further investigation I just realized, that the app only crashes if it is started from within xcode (on device).

When it is started directly on the device, everything is working fine.

Anyways. Being able to debug is a must, so the topic is still relevant.

Accepted Answer

Does this still happen if you disable Metal API Validation in your build scheme?

Thank you so much! That solved my problem.

Can you explain to me what is happening there and why the memory usage spikes to many gigabytes during context.writeJPEGRepresentation when the Metal API Validation is enabled?

Could be many things, I wouldn't want to speculate. I do recommend that you file a bug report about it at https://developer.apple.com/bug-reporting/

App crashes while trying to save CIImage after applying CIFilter
 
 
Q