CGImageDestinationFinalize crashed iOS15.2

I wrote the code as below for save image from PHImageManager, but a crash occurs. The data is from PHImageManager.default().requestImageDataAndOrientation.

Crash Code

guard let cgImage = UIImage(data: data)?.cgImage else { return }
let metadata = ciImage.properties
let destination: CGImageDestination = CGImageDestinationCreateWithURL(url as CFURL, uti as CFString, 1, nil)!
CGImageDestinationAddImage(destination, cgImage, metadata as CFDictionary?)
let success: Bool = CGImageDestinationFinalize(destination) // <- crashed

Not Crash Code

guard let cgImage = UIImage(data: data)?.cgImage else { return }
let metadata = ciImage.properties
let destination: CGImageDestination = CGImageDestinationCreateWithURL(url as CFURL, uti as CFString, 1, nil)!
CGImageDestinationAddImage(destination, cgImage, nil)
let success: Bool = CGImageDestinationFinalize(destination) // <- not crashed

metadata

{
    ColorModel = RGB;
    DPIHeight = 72;
    DPIWidth = 72;
    Depth = 8;
    PixelHeight = 2160;
    PixelWidth = 2880;
    ProfileName = "sRGB IEC61966-2.1";
    "{Exif}" =     {
        ApertureValue = "1.356143809255609";
        BrightnessValue = "0.1278596944592232";
        ColorSpace = 1;
        ComponentsConfiguration =         (
            1,
            2,
            3,
            0
        );
        CompositeImage = 2;
        DateTimeDigitized = "2021:12:28 08:38:28";
        DateTimeOriginal = "2021:12:28 08:38:28";
        DigitalZoomRatio = "1.300085984522786";
        ExifVersion =         (
            2,
            2,
            1
        );
        ExposureBiasValue = "0.09803208290449658";
        ExposureMode = 0;
        ExposureProgram = 2;
        ExposureTime = "0.025";
        FNumber = "1.6";
        Flash = 16;
        FlashPixVersion =         (
            1,
            0
        );
        FocalLenIn35mmFilm = 33;
        FocalLength = "4.2";
        ISOSpeedRatings =         (
            400
        );
        LensMake = Apple;
        LensModel = "iPhone 12 back camera 4.2mm f/1.6";
        LensSpecification =         (
            "4.2",
            "4.2",
            "1.6",
            "1.6"
        );
        MeteringMode = 5;
        OffsetTime = "+09:00";
        OffsetTimeDigitized = "+09:00";
        OffsetTimeOriginal = "+09:00";
        PixelXDimension = 2880;
        PixelYDimension = 2160;
        SceneCaptureType = 0;
        SceneType = 1;
        SensingMethod = 2;
        ShutterSpeedValue = "5.321697281908764";
        SubjectArea =         (
            2011,
            1509,
            2216,
            1329
        );
        SubsecTimeDigitized = 686;
        SubsecTimeOriginal = 686;
        WhiteBalance = 0;
    };
    "{IPTC}" =     {
        DateCreated = 20211228;
        DigitalCreationDate = 20211228;
        DigitalCreationTime = 083828;
        TimeCreated = 083828;
    };
    "{JFIF}" =     {
        DensityUnit = 0;
        JFIFVersion =         (
            1,
            0,
            1
        );
        XDensity = 72;
        YDensity = 72;
    };
    "{TIFF}" =     {
        DateTime = "2021:12:28 08:38:28";
        HostComputer = "iPhone 12";
        Make = Apple;
        Model = "iPhone 12";
        Orientation = 0;
        ResolutionUnit = 2;
        Software = "Snowcorp SODA 5.4.8 / 15.2";
        XResolution = 72;
        YResolution = 72;
    };
}

What are the reasons?

If I use CGimageDestinationAddImageFromSource instead of CGimageDestinationAddImage, there is no crash even if I add metadata.

If I use PHImageManager.default().requestImage instead of PHImageManager.default().requestImageDataAndOrientation, and extract cgImage, there is no crash even if I add metadata.

Can you show your crash stack ?

Hi.

I had the same problem and I found out that the metadata contains an invalid orientation value which result in the crash you described. I can reproduce it by setting the orientation to 0.

The cash helped me to find the problem IIOGetExifOrientation + 48

 "{TIFF}" =     {
        .....
        Orientation = 0;
}

Only 1-8 are valid values for the orientation: https://www.awaresystems.be/imaging/tiff/tifftags/orientation.html

Thank you so much Nils.CEWE for this hint regarding the orientation '0' being not accepted! Now at least I know why it keeped crashing randomly. I need to implement a fix now ;-)

CGImageDestinationFinalize crashed iOS15.2
 
 
Q