It appears that:
[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png"];
Does not return anything for assets that are inside the Images.xcassets asset catalog. I also tried:
[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images"];
[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images.xcassets"];
But neither of those worked either.
Has anyone had success retrieving paths to assets in the catalog?
An asset catalog is a type of file used to organize and manage different assets and image resolutions used by your app's user interface.
If you don't already have an asset catalog in your project, you can create one by right-click on your project and choosing New File. From "iOS" choose "Resource" then Asset Catalog, then click Next and name your catalog. You can now select your new asset catalog in Xcode, and drag pictures directly into it.
An asset catalog, simply put, is a single folder in Xcode that you use to organize your app's images, icons, colors, and more. Instead of adding individual images to Xcode's file organizer, you add assets neatly organized in a single catalog. Asset catalogs also include metadata, for example about image scale.
Same problem, but I don't think we could access it via pathForResource:
, except below:
UIImage* image = [UIImage imageNamed:@"name-in-asset-catalog"];
It has been clearly documented:
Each set in an asset catalog has a name. You can use that name to programmatically load any individual image contained in the set. To load an image, call the UIImage:imageNamed: method, passing the name of the set that contains the image.
I found that in the package of complied app, a file named "Assets.car" came out, and I think it's the entire images sets in my project, and should have been zipped or something.
Refer to Apple's documentation:
Xcode 5 provides different functionality for asset catalogs depending on the deployment target for your project:
- For all projects, individual images can be loaded using set names.
- For projects with a deployment target of iOS 7, Xcode compiles your asset catalogs into a runtime binary file format that reduces the download time for your app.
So that's it.
I'm also looking for a way that doesn't use imageNamed:
, I don't want runtime to cache my images.
I wanted to access some vector assets for creating UNNotificationAttachment
with local resources so I came up with this helper class. It basically just gets image from assets, saves its data to disk and return file URL. I hope that helps someone.
import UIKit class AssetExtractor { static func createLocalUrl(forImageNamed name: String) -> URL? { let fileManager = FileManager.default let cacheDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0] let url = cacheDirectory.appendingPathComponent("\(name).png") guard fileManager.fileExists(atPath: url.path) else { guard let image = UIImage(named: name), let data = UIImagePNGRepresentation(image) else { return nil } fileManager.createFile(atPath: url.path, contents: data, attributes: nil) return url } return url } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With