path of suporting files in OS X program? - objective-c

I need some help for my OS X program.
I need the URL of a file inside the supporting files.
I have an array in which I save URLs from images and add them to a table view and if no images are chosen I want to add a question mark image (it is called "bild.jpg")
This bild.jpg is inside the supporting files but for later use I can't just save the name of the image because the array stores also URLs.
I need to have the URL of that image in the supporting file because it's easier to use the array for image initialization.
Is there a function to get the path or is there a standard path to the supporting files? I already search on the net but couldn't find anything that could help.

You seem to be talking about the application bundle and its resources directory rather than, say, a subdirectory in ~/Library/Application Support/..., in which case you probably want something like:
[[NSBundle mainBundle] URLForResource:#"bild" withExtension:#"jpg"]
(See the documentation for NSBundle.)

Related

How to build with a custom eglfs cursor atlas?

I'm trying to change the eglfs mouse cursor graphics for my embedded linux QT application (QT5.5). I have the new cursor atlas PNG and the new JSON descriptor file, but the documentation is rather vague:
".. a custom cursor atlas can be provided by setting the QT_QPA_EGLFS_CURSOR environment variable to the name of a JSON file. The file can also be embedded into the application via Qt's resource system."
I'd prefer to keep everything within the resource system if possible but I can't work out how to do it.. do I need a specific qrc file containing the path to the JSON file? I assume that the PNG file would also need to be added as a resource so that it gets built into the application?
If adding it via the resource system is a bad idea where's the correct place to set the QT_QPA_EGLFS_CURSOR environment variable? I'm currently specifying the platform on the command line via "-platform eglfs"; will this be ok or will I need to set the platform to eglfs in the build?
After much trial, error and digging around I have found the solution that I was looking for within the resource system.
Create a new resource file called "cursor.qrc", the contents of which needs to be two lines:
path/to/your/custom-cursor-atlas.png
cursor.json
The first line (path to your cursor atlas) must be relative to your resource directory.
You then need to put the JSON file (contents as described in the documentation) in the root of your resource directory. It must be called "cursor.json", and its image location line must must match the location in your new resource file and be of the format:
"image": ":/path/to/your/custom-cursor-atlas.png",
This will then include your cursor atlas in resources, and Qt will find it when your application starts.
Run time solution example:
export XDG_RUNTIME_DIR=~
export QT_QPA_EGLFS_CURSOR=~/cursor.json
In the cursor.json:
"image": "cursor.png",
Put your custom cursor.png atlas into your home dir (~) then run the Qt app from there.

iOS file system: adding/removing/sorting files

I have a directory with images like this:
0000.png
0001.png
0002.png
0003.png
etc...
To add an image, I query the count of the directory and name the new file with the count. I need to maintain such an order because these images are eventually displayed in a grid view. This is the easy part. What is bugging me is manipulating the directory later.
The user can delete and reorder these as they wish. So how should I handle reordering of the actual directory?
I have tried, naively, copying the images into an NSMutableArray, loading those into the gridView, letting the user manipulate, then upon finish I wipe the directory and rewrite the images. That's fine for a few images, but there can be many and it slowly builds up.
I like the idea of copying the images locally, manipulating and then fixing the directory. But I think there has got to be a better way to manage the directory instead of wiping and rewriting, would it be faster to write some kind of file-swapping method?
Instead of writing the images to the file system in an ordered format, you could have a meta data file images.plist
And maintain an NSMutableArray of NSDictionaries
Each dictionary will contain the data for the imageFile Path and the image index
Then when you would save the changes of the user you will only rewrite this plist file, instead of saving all the images again

Can I use the iOS .strings files for anything other than localization?

I would like to put all the images that I use in my application into the same file and access them by key.
Is it possible to use the .strings file for this?
Assuming you mean you want to put the image names, yes it's possible.
In your code, just use things like [UIImage imageNamed:NSLocalizedString(key, #"")];
Where key is the key that points to the name of your image. Then in the .strings files, point to the right image file based on the locale.
Yes you can! I use it to list website URLs used in my app. This makes it extremely easy to change in the app, if the website URL ever changes. There is no problem with doing this.

the physical path for my files inside the Resources folder of XCode (for IOS)

I'm developing an application for iPad. I want to access a text file inside the Resources folder! I'm doing that in following way:
NSString* filePath = #"/Users/net4uonline/Desktop/slots2/paylines.txt";
Now, if I move my whole project from Desktop to somewhere else I know this won't work. So, is there any way to give a relative path for this file instead of the current path. Maybe like the following?
NSString* filePath = #"Resources/paylines.txt";
I know this won't work but as my file is always going to reside inside the Resources folder, so I thought this might work!
I guess you are looking for NSBundle and pathForResource:ofType: method, aren't you?

Show contents of a zip file in a WebView

I want to have a WebView that displays some static files from the application bundle. Since I have a large number of small files, I'd like to pack them all into a compressed archive so the application doesn't take up too much space. What's the best way to make this happen?
This should help you out: http://code.google.com/p/ziparchive/
To display data in the WebView:
On Mac OS X use WebFrame's loadHTMLString:baseURL:
On iOS use UIWebView's loadHTMLString:baseURL:
What you probably want to do, is implement an NSURLProtocol subclass that will resolve relative URLs by reading them from the zip archive. That way, you only need to initially read the "main" HTML file from the zip into memory, and the others will be read in on demand. To get WebKit to use your custom URL protocols for resolving relative paths, you could instantiate the WebView like this:
[[web_view mainFrame] loadHTMLString:your_main_html baseURL:[[NSURL alloc] initWithString:#"zip:///"]];
Apple has a really good example of combining a custom URL protocol with a WebView here:
https://developer.apple.com/library/mac/#samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816