From fda05a36223ac78be19fd57e2caa553cfec22094 Mon Sep 17 00:00:00 2001 From: Licardo_du Date: Fri, 29 May 2026 17:08:32 +0800 Subject: [PATCH] Merge pull request #29138 from Licardo-du:fix-imgcodecs-apple-avfoundation imgcodecs: fix Apple conversions build without AVFoundation #29138 Fixes #28553. This PR fixes Apple imgcodecs conversion builds when AVFoundation is disabled or unavailable on older macOS SDKs. Changes: - Guard AVFoundation import with HAVE_AVFOUNDATION. - Use older macOS-compatible framework imports when ImageIO is unavailable. - Add __bridge compatibility for older Objective-C++ compilers. - Use NSMakeSize for older macOS instead of passing CGSize to NSImage API. Testing: - Not tested locally: no macOS environment available. - Patch follows the fix confirmed in #28555 discussion. --- modules/imgcodecs/src/apple_conversions.h | 17 +++++++++++++++++ modules/imgcodecs/src/apple_conversions.mm | 6 ++++++ modules/imgcodecs/src/macosx_conversions.mm | 4 +++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/modules/imgcodecs/src/apple_conversions.h b/modules/imgcodecs/src/apple_conversions.h index c68b922cfb..65d3a7e08b 100644 --- a/modules/imgcodecs/src/apple_conversions.h +++ b/modules/imgcodecs/src/apple_conversions.h @@ -2,10 +2,27 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. +#ifndef OPENCV_IMGCODECS_APPLE_CONVERSIONS_H +#define OPENCV_IMGCODECS_APPLE_CONVERSIONS_H + #include "opencv2/core.hpp" #import + +#include + +#ifdef HAVE_AVFOUNDATION #import +#endif + +#if TARGET_OS_IPHONE || (defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1080) #import +#else +#import +#import +#import +#endif CV_EXPORTS CGImageRef MatToCGImage(const cv::Mat& image) CF_RETURNS_RETAINED; CV_EXPORTS void CGImageToMat(const CGImageRef image, cv::Mat& m, bool alphaExist); + +#endif // OPENCV_IMGCODECS_APPLE_CONVERSIONS_H \ No newline at end of file diff --git a/modules/imgcodecs/src/apple_conversions.mm b/modules/imgcodecs/src/apple_conversions.mm index 6126039ce0..6b1834504b 100644 --- a/modules/imgcodecs/src/apple_conversions.mm +++ b/modules/imgcodecs/src/apple_conversions.mm @@ -5,6 +5,12 @@ #include "apple_conversions.h" #include "precomp.hpp" +#import + +#ifndef __bridge +#define __bridge +#endif + CGImageRef MatToCGImage(const cv::Mat& image) { NSData *data = [NSData dataWithBytes:image.data length:image.step.p[0] * image.rows]; diff --git a/modules/imgcodecs/src/macosx_conversions.mm b/modules/imgcodecs/src/macosx_conversions.mm index 0023e06260..960139e2f1 100644 --- a/modules/imgcodecs/src/macosx_conversions.mm +++ b/modules/imgcodecs/src/macosx_conversions.mm @@ -13,9 +13,11 @@ NSImage* MatToNSImage(const cv::Mat& image) { CGImageRef imageRef = MatToCGImage(image); // Getting NSImage from CGImage - NSImage *nsImage = [[NSImage alloc] initWithCGImage:imageRef size:CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef))]; + NSSize imageSize = NSMakeSize(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); + NSImage *nsImage = [[NSImage alloc] initWithCGImage:imageRef size:imageSize]; CGImageRelease(imageRef); + return nsImage; }