Load Image from Bundle with iOS

Load image from bundle with iOS

That is correct, imageNamed: will search your main bundle. Images in your project, even if they are in different groups in your Project Navigator will be in your main bundle and can be accessed directly by name.

How to Use Bundle In SwiftUI with Image

The SwiftUI Image(_ , bundle: _) looks for image resource in corresponding bundle's Assets catalog. In your case the image is just embedded as regular file, so you have to find and load it as file. Image itself cannot do that, so it should be constructed with UIImage that has such possibility.

So, assuming you Game.bundle is in PlugIns subfolder of main bundle (if not - just correct corresponding path construction below) here is possible approach.

Tested with Xcode 12 / iOS 14

struct ContentView: View {
var body: some View {
Image(uiImage: gameImage(name: "test") ?? UIImage())
}

func gameImage(name: String, type: String = "png") -> UIImage? {
guard let plugins = Bundle.main.builtInPlugInsPath,
let bundle = Bundle(url: URL(fileURLWithPath:
plugins).appendingPathComponent("Game.bundle")),
let path = bundle.path(forResource: name, ofType: type)
else { return nil }
return UIImage(contentsOfFile: path)
}
}

How to load images from a bundle in iOS using Swift

This has nothing to do with the bundle. The problem is merely that a PDF is not an image. It is a PDF. UIImage(contentsOfFile:) wouldn't work even if the PDF file were in your normal app bundle.

Loading images from assets folder from a bundle

Two things to check.

1) make sure your assets bundle has an Info.plist with a bundle identifier set in it. From your screenshot it doesn't look like this is the case.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>MyBundle</string>
</dict>
</plist>

2) Make sure your bundle has a compiled assets catalog in it. Should be named Assets.car. I don't think Xcode will just compile an Assets.xcassets folder inside a resource bundle (the bundle is effectively opaque to Xcode; likewise it wouldn't compile any .m source files you put in there). You may need a custom build step to copy the compiled Assets.car file into your assets bundle before the assets bundle is copied into the app bundle.

You can check these by finding your app bundle and right clicking on it then "Show Package Contents", then again on the contained bundle.

Manual assets compilation command:

xcrun actool Images.xcassets --compile . --platform iphoneos --minimum-deployment-target 9.0

Load image from iOS 8 framework

While @Renatus answer is perfectly valid and addresses the core issue (bundle for framework needs to be specified), I wanted to post the solution I went with since it's slightly more direct:

Swift 3.0/4.0/5.0

let image = UIImage(named: "YourImage", in: Bundle(for: YOURFRAMEWORKCLASS.self), compatibleWith: nil)

Alternatively, you can use this pattern for non-class, aka non-"static", functions:

let image = UIImage(named: "YourImage", in: Bundle(for: type(of: self)), compatibleWith: nil)

or this pattern for class functions:

let image = UIImage(named: "YourImage", in: Bundle(for: self), compatibleWith: nil)

These alternatives are better for cutting and pasting.

Cocoapods can't load image from Bundle

 s.resources = 'Hashtags/Assets/**/*.{png,storyboard}'
s.resource_bundle = { 'Hashtags' => 'Hashtags/Assets/*.png' }

This is the only thing that worked... Combined to the code I pasted

let view = UIImageView(frame: self.bounds)
let bundle = Bundle(for: Test.self)
let image = UIImage(named: "close", in: bundle, compatibleWith: nil)


Related Topics



Leave a reply



Submit