简体   繁体   中英

How to bundle/store application image data in my OpenCV iOS application?

I am new to Xcode and iOS development and I have a basic question about how to store (bundle) application data that consist of images needed by my application. My application requires a small database of images as input that I supply.

I have an Xcode project in C++ that uses OpenCV that is currently compiled and running correctly on my Mac. The application on my Mac simply reads the image data it needs from a folder on my file system that I can easily point to. I am trying to port this application to iOS using either Objective-C or Swift. I was able to write some basic Objective-C code as a wrapper to my C++/OpenCV application but I am now at the point where I need to access the iOS file system to read the images and I am not sure where to locate that data and how to configure my Xcode project to include it.

After doing some reading on this topic I see that there are several ways to store data in iOS, but I am uncertain about what approach would be appropriate and relatively easy to implement. My understanding is that all the data for my app needs to live in the application sandbox. I see plenty of examples for how to get the file path for various folders in the sandbox, but it is not clear to me how to actually configure my project to include the data (ie, where do I put my images?). Is there something I need to configure within my Xcode project so that when I compile the application it knows about my data?

I found many posting about the iOS file system, Core data, archiving data, etc… but had a hard time locating any information about how to actually configure my project with data that I supply. Any help would be greatly appreciated.

Thank you!

If the images that you are supplying are fixed in the app (ie- not changing over time) then you simply add the images to the app itself. They are stored in the app bundle and can be accessed as follows (with sample names);

    let imagePath = NSBundle.mainBundle().pathForResource("myImage", ofType:"jpg")  
    let splashImageURL = NSURL.fileURLWithPath(imagePath!)
    anImage = NSImage(contentsOfURL:splashImageURL)

You can create a folder in the project to hold the images. This is not used in the final image path, only for convenience in the project. You can use the File menu or drag and drop image files directly into the project. Make sure to check the 'copy files' option to move the images from your source location into the project itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM