I have a cocoapod library which contains assets in 2 formats:
my podsp
Well, image asset catalogs are not supported via pods - just include a resource bundle that contains your image files in your podspec like so:
s.subspec 'Resources' do |resources|
resources.resource_bundle = {'MyBundle' => ['Resources/**/*.{png}']}
end
and access the images safely from the pod like that:
+(NSBundle *)getResourcesBundle
{
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"MyBundle" withExtension:@"bundle"]];
return bundle;
}
+(UIImage *)loadImageFromResourceBundle:(NSString *)imageName
{
NSBundle *bundle = [MyClass getResourcesBundle];
NSString *imageFileName = [NSString stringWithFormat:@"%@.png",imageName];
UIImage *image = [UIImage imageNamed:imageFileName inBundle:bundle compatibleWithTraitCollection:nil];
return image;
}
That solves all issues of handling images/resources within a cocoapod.
Only this works for me (Swift 3 & Swift 4)
public struct ImagesHelper {
private static var podsBundle: Bundle {
let bundle = Bundle(for: YourClass.self)
return Bundle(url: bundle.url(forResource: "YourClass",
withExtension: "bundle")!)!
}
private static func imageFor(name imageName: String) -> UIImage {
return UIImage.init(named: imageName, in: podsBundle, compatibleWith: nil)!
}
public static var myImage: UIImage {
return imageFor(name: "imageName")
}
}
Then use it as below:
ImagesHelper.myImage
As in the @Chris Prince's answer, don't forget to update your podspec file like:
s.resource_bundles = {
'YourClass' => ['YourPodName/*/Assets.xcassets']
}
I can't still get it to work, with none of the solutions above. I have an Assets.xcassets file in my Pod.
And inside my Pod classes i would like to access images from the Assets
Specified like this:
s.resource_bundles = {
'SWDownloadPlayButton' => ['SWDownloadPlayButton/Assets/Assets.xcassets']
}
With this helper:
public struct ImagesHelper {
private static var podsBundle: Bundle {
let bundle = Bundle(for: SWDownloadPlayButton.self)
return Bundle(url: bundle.url(forResource: "SWDownloadPlayButton",
withExtension: "bundle")!)!
}
private static func imageFor(name imageName: String) -> UIImage {
return UIImage.init(named: imageName, in: podsBundle, compatibleWith: nil)!
}
public static var download_icon: UIImage {
return imageFor(name: "download_icon")
}
}
But whatever i do inside my Pod classes (not in the example project)...like this
var centerImage: UIImage = ImagesHelper.download_icon.withRenderingMode(.alwaysTemplate) {
didSet {
updateImage()
}
}
I still get a nil on the centerImage
At least as of version 1.0.1 of Cocoapods, image asset catalogs are supported.
In my Swift 4.2 code, I used:
public static var GoogleIcon: UIImage {
let bundle = Bundle(for: self)
log.msg("bundle: \(bundle)")
return UIImage(named: "GoogleIcon", in: bundle, compatibleWith: nil)!
}
In my pod spec, I used:
s.resources = "SMCoreLib/Assets/*.xcassets"
When I build using the simulator, the .car file does show up in the Framework:
cd /Users/<snip>/SMCoreLib.framework
ls
Assets.car Info.plist SMCoreLib
Swift 3 version
private func getImageFromBundle(name: String) -> UIImage {
let podBundle = Bundle(for: YourClass.self)
if let url = podBundle.url(forResource: "YourClass", withExtension: "bundle") {
let bundle = Bundle(url: url)
return UIImage(named: name, in: bundle, compatibleWith: nil)!
}
return UIImage()
}
If you're using SwiftUI, using the Image.init(_ name: String, bundle: Bundle?)
initializer won't work, you have to initialize it like this:
func getImage(named name: String) -> UIImage {
// Use a random singleton class in your project.
// Emphasis on class, structs won't work.
let bundle = Bundle(for: [random class].self)
if let image = UIImage(named: name, in: bundle, compatibleWith: nil) {
return image
}
return .init()
}
Image(uiImage: getImage(named: "image_name"))