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.
How to use image contained in Resource framework from main module?
As of Xcode 12.1, I do not believe there is a way to do this.
The reason is that when you configure an images using storyboards Xcode defaults to assuming those images are located in the bundle for the current module, and the Attributes inspector does not provide a way to specify an alternate module, as it does in the case of view controller classes:
View controller attribute inspector with Module property
But in code this is easy to accomplish using the in:Bundle?
parameter for initializing a UIImage
, which tells the app to look in a specific bundle for the image resource at runtime.
guard
let bundleURL = Bundle.main.url(forResource: "[bundle filename without extension]", withExtension: "bundle"),
let bundle = Bundle(url: bundleURL) else { return }
let image = UIImage.init(named: "[image name]", in: bundle, compatibleWith: nil)
It is likely an oversight on Apple's part to allow Xcode to show image resources from other modules in the image name field, but not properly link them when compiling storyboards. I hope they fix this.
Beware of using color assets from other modules as well:
While they appear to work, it is only because when you assign color assets in a storyboard, Xcode actually encodes the color information from the asset in the storyboard file itself. One sign this is not intended to work is that if you define a Color Set
asset with a variation for Dark Appearance, only the Any Appearance color will be used (even in dark mode). Color Set
assets in the same module, however, work normally.
Color Set Asset with Any Appearance and Dark Appearance
How to load images in a Cocoa Touch framework?
We are creating framework for using in another projects, So in your framework you want to access your frameWork bundle get the frameWork bundle by below code.
func getBundle() -> Bundle? {
var bundle: Bundle?
if let urlString = Bundle.main.path(forResource: "YOUR_FRAMEWORK_BUNDLE_NAME", ofType: "framework", inDirectory: "Frameworks") {
bundle = (Bundle(url: URL(fileURLWithPath: urlString)))
}
return bundle
}
let bundel = getBundle()
imageView.image = UIImage(named: "image.jpg", in: bundel, compatibleWith: nil)
How to develop an iOS framework which includes image resources?
At last, I create a bundle to hold all image resources, and we provide *.framework and *.bunlde, that's more elegant.
PhotoKit iOS8 - Retrieve image using the PHImageFileURLKey
I think your solution of retrieving Photo Kit asset from the URL is wrong.
Here is what I would do (supposing you have access to PHAsset):
Store the localIdentifier:
PHAsset *asset = /*Your asset */
NSString *localIdentifier = asset.localIdentifier;
//Now save this local identifier, or an array of them
When it is time to retrieve them you simply do:
PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:savedLocalIdentifiers options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
//this gets called for every asset from its localIdentifier you saved
}];
If you only have access to “PHImageFileURLKey” then disregard this answer.
ios 8, how to get all photos using ALAssetLibrary to replace the missing camera roll album
Alright, so seems like it was just a bug, and iOS 8.0.2 fixed it (at least for me, as I can now see all the photos in the image picker).
This release contains improvements and bug fixes, including:
[...]
Fixes an issue that prevented some apps from accessing photos from the Photo Library. [...]
iOS 8: get resource from Cocoa Touch Framework
Try this:
NSBundle *frameworkBundle = [NSBundle bundleForClass:[AnyClassFromFramework class]];
Related Topics
Uicollectionview - Resizing Cells on Device Rotate - Swift
Dispatchsourcetimer and Swift 3.0
Swift Watchos 2 - Cmsensordatalist
What Causes 'Constant Captured by a Closure Before Being Initialized' Error
Swift - Could Not Cast Value of Type '_Nscfstring' to 'Nsdictionary'
Swift - Unit Testing Private Variables and Methods
A Swift Protocol Requirement That Can Only Be Satisfied by Using a Final Class
Generating Resource_Bundle_Accessor, Type 'Bundle' Has No Member 'Module'
How to Include .Swift File from Other .Swift File in an Immediate Mode
Swift Generics: Requiring Addition and Multiplication Abilities of a Type
Swift: Determine What Object Called a Function
Split a String Without Removing the Delimiter in Swift
Binary Operator '/' Cannot Be Applied to Two 'Double' Operands
Propagate an Optional Through a Function (Or Init) in Swift
Simple Swift Fibonacci Program Crashing (Project Euler 2)
How to Init a Uibutton Subclass
How to Find the Number of Days in Given Month and Year Using Swift