OpenCV with SwiftUI

Hi, I am trying to develop an App using SwiftUI together with OpenCV framework. After I built the app, the ContentView has a warning to import opencv2. the warning is ":'/Users/kinsuenlau/Downloads/opencv2.framework/Modules/opencv2.swiftmodule/Project/x86_64-apple-ios-simulator.swiftsourceinfo' is either malformed or generated by a different Swift version. Note that it uses an unstable format and may leak internal project details, it should not be distributed alongside modules"

My MacBook is Apple M1 chip
Xcode: 14.3.1
iPhone: 14 max pro
OpenCV: 4.8 (has tried 4.7, 4.54 with same warning)

I am much appreciate for any suggestion to this warning from this forum.

1 Like

This unfortunately means that the XCFramework is incompatible with your code, and short of jumping back to an older Xcode and iOS target, there's not much you can do with the XCFramework you've downloaded.

In the case of openCV, I'd suggest building your own XCFramework (building from Sources, rather than using the iOS pack download). Looking at the recent 4.8 release (which came out July 2, 2023), the XCFramework was built with Swift 5.4, targeting iOS 9- which is notably older than current iOS releases. Assuming you're targeting iOS16 or maybe iOS17, it's quite possible the older XCFramework is just incompatible with current releases.

The targeted build detail I got from manually inspecting the .swiftinterface file in the downloaded "iOS pack" (XCFramework) - specifically ./Versions/A/Modules/opencv2.swiftmodule/x86_64-apple-ios-simulator.swiftinterface, which starts with the following:

// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)
// swift-module-flags: -target x86_64-apple-ios9.0-simulator -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -O -module-name opencv2

Digging around in Google a bit, I found this gist that details someone else wanting to basically do this process, which you might find useful: Make opencv2.xcframework for iOS arm64, and iOSSimulator arm64 & x86_64 · GitHub

NOTE: I haven't tried this myself, and haven't used OpenCV's build tools, so you'll need to judge for yourself what's reasonable there. By default, I'd tend to try the build techniques directly in the repository or documentation and adjust as needed from there. The obvious pointed I found in OpenCV's documentation was OpenCV: Installation in iOS

3 Likes

Thank you Joseph. Let me try your suggestion.