Since both the SDK version and the compiler version are tied to an Xcode release, you can use the #if compiler directive as a stand-in for the SDK version:
import AVFoundation
extension AVCapturePhoto {
func stupidOSChangePreviewCGImageRepresentation() -> CGImage? {
#if compiler(>=5.5)
// iOS 15+ SDK, macOS 12+ SDK
return self.previewCGImageRepresentation()
#else
return self.previewCGImageRepresentation()?.takeUnretainedValue()
#endif
}
}