Can I access an xcassets directory on the filesystem?
Asked Answered
E

4

7

I would like to dynamically load all images in an xcassets directory. The files are named StockPhoto# where # is the number in the list. If I can access my StockPhotos.xcassets at runtime to count all the files in the directory, I won't have to manually load the files each time I add new stock photos.

If there are other solutions to this problem, I'm open to that but I'm also just very curious how xcassets are handled by the file system- whether they're just reference to a set of files, or actually their own directory. Information on this is sparse.

Eyecup answered 20/3, 2014 at 0:12 Comment(0)
P
5

If there are other solutions to this problem, I'm open to that

The problem is that there is no introspection at runtime into an asset catalog: it isn't a "thing" you can "see" as far as Objective-C and Cocoa Touch are concerned.

The usual solution to this kind of problem is to drag a folder full of images into your project at the outset, and when you do, choose "Create folder references for any added folders" in the dialog - not "Create groups for any added folders". The result is that the folder is copied into your app bundle, and now you can use ordinary file system methods to say "every file in this folder".

Pock answered 20/3, 2014 at 0:32 Comment(4)
That's what I was afraid of. Too bad. So if I build the app with a folder for these photos- is it going to be placed at the app's home directory, Documents, or Library?Eyecup
None of the above. It will be in the main bundle like any built in resource.Pock
That's what I meant by home but I should've clarified. Thanks for the pointers, matt.Eyecup
No problem. I've done the exact thing you are describing (adding more and more images to the folder as development of the app proceeds, and fetching them dynamically as the app runs by looking in the folder) so I know it can be done this way.Pock
S
5

Upon compilation of your iOS project, xcassets are compiled to produce either image files, or a proprietary .car file. In that latter case images won't be stored in a directory you can browse.

If your "Deployment Target" is less that iOS7 (meaning that your app would still be able to run on iOS6)

  • It will produces the same set of image files that you would have had to produce without using Assets Catalog, namely <YourImageName>.png, <YourImageName>@2x.png, <YourImageName>~ipad.png, <YourImageName>[email protected] and so on, for each image set of your xcassets.

If your "Deployment Target" is iOS7 or greater (meaning that your app would only be able to run on iOS7+)

  • It will produce a single big .car file in the final bundle (I don't really looked up if this file was actually an sqlite3 datatbase or some proprietary format or whatnot, but who cares, you are not supposed to manipulate it anyway). This big .car file contains all the images, with all their provided variants, and even with slicing info (if you did slice some of them for tiling or to use them as 9-patch images using the tool provided for that in the Assets Catalog editor)

Whatever the produced result you shouldn't / are not supposed to dig into internal details of your bundle like that. The format of the .car file may even change from one iOS version to another (who knows? that's internal details after all which we shouldn't have to deal with) so don't base your logic on it.

[EDIT]: If you need to be sure to have a directory with your set of images at the end of the compilation, you could instead use a folder reference (referencing a real folder in the Finder, as opposed to an Xcode "group" as only group files in Xcode's Project Navigator) then use code to browse it. But then you will have to deal with other details, like only browse files that match the current device (iPhone vs. iPad, non-retina vs. retina…), so this would only shift the problem further in your case; you really should use a constant somewhere to declare the number of images (or put this in some PLIST file for example) and iterate thru them.

As the files you provide at compile time will be in your Bundle — which cannot be altered once compiled as it is digitally signed — the number of images will never changed once the app is compiled anyway. (That's not like if you used the Documents directory and enabled iTunes File Sharing or whatever, letting the user add images himself ;-))

Suffumigate answered 20/3, 2014 at 0:26 Comment(0)
P
5

If there are other solutions to this problem, I'm open to that

The problem is that there is no introspection at runtime into an asset catalog: it isn't a "thing" you can "see" as far as Objective-C and Cocoa Touch are concerned.

The usual solution to this kind of problem is to drag a folder full of images into your project at the outset, and when you do, choose "Create folder references for any added folders" in the dialog - not "Create groups for any added folders". The result is that the folder is copied into your app bundle, and now you can use ordinary file system methods to say "every file in this folder".

Pock answered 20/3, 2014 at 0:32 Comment(4)
That's what I was afraid of. Too bad. So if I build the app with a folder for these photos- is it going to be placed at the app's home directory, Documents, or Library?Eyecup
None of the above. It will be in the main bundle like any built in resource.Pock
That's what I meant by home but I should've clarified. Thanks for the pointers, matt.Eyecup
No problem. I've done the exact thing you are describing (adding more and more images to the folder as development of the app proceeds, and fetching them dynamically as the app runs by looking in the folder) so I know it can be done this way.Pock
P
1

If you're targeting iOS 7+ then no. Xcode will package the files into a proprietary format (.car) that you can't access directly.

Either use imageNamed: methods, or don't use Image Catalogs for the files you need to access directly.

Pitch answered 20/3, 2014 at 0:23 Comment(0)
C
0

as @AliSoftware suggests you can store all assets images to plist and access them later for more details see here

Cand answered 6/10, 2016 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.