Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using previewCGImageRepresentation in both Xcode 13 and Xcode 12?

I’ve run into an issue where Xcode 13b2 (iOS 15 SDK) changed the Swift return type of AVCapturePhoto.previewCGImageRepresentation(). In Xcode 12.5.1 (iOS 14 SDK), this method returns Unmanged<CGImage>. In 13b2 - 13b4, it returns CGImage?.

I need my code to compile under both Xcode versions, since Xcode 13 has other issues, and can’t be used to submit builds to the App Store. I thought I was clever writing this, but it won’t compile, because it’s not conditional code compilation check but rather a runtime check:

extension AVCapturePhoto {
    func stupidOSChangePreviewCGImageRepresentation() -> CGImage? {
        if #available(iOS 15, *) {
            return self.previewCGImageRepresentation()
        } else {
            return self.previewCGImageRepresentation()?.takeUnretainedValue()
        }
    }
}

Another possibility might be to create a user-defined Xcode setting, but I don’t think that can be done conditionally based on Xcode or SDK version.

There might be some unsafe pointer histrionics one can do…

Any other ideas?

like image 514
Rick Avatar asked Mar 10 '26 19:03

Rick


1 Answers

You can make a check on the swift compiler version. An exhaustive list is available on wikipedia at the time of writing.

https://en.wikipedia.org/wiki/Xcode#Toolchain_versions

extension AVCapturePhoto {
    func stupidOSChangePreviewCGImageRepresentation() -> CGImage? {
    #if compiler(>=5.5)
        return self.previewCGImageRepresentation()
    #else
        return self.previewCGImageRepresentation()?.takeUnretainedValue()
    #endif
    }
}
like image 126
Shinnyx Avatar answered Mar 13 '26 07:03

Shinnyx