Reputation: 155
Goal - Create a framework "F" with UI Component and Image assets inside framework. I am using SwiftUI Framework returns a UI component with some images to user of this framework. User doesn't need to know anything what image he is going to get from framework.
Expectation - When User integrate this framework using Carthage and call UI api from framework he should get a UI view with some text and image.
Actual - User is getting the text but Images are not coming up.
Error - TestApp[87355:3127354] [SwiftUI] No image named 'Apple' found in asset catalog for /Users/AT09/Library/Developer/CoreSimulator/Devices/C6BCA75A-DE25-4F7D-992F-AED8412B483C/data/Containers/Bundle/Application/B7E13C38-0ED0-4565-AC4B-3527582DEB09/TestApp.app
I tried creating bundle as suggested in this thread, but it doesn't work.
Code to load Image in Framework, "Apple" is Image in .xcassests in framework
Image("Apple") .renderingMode(.original) .resizable() .scaledToFit() .padding(.vertical, 20).padding(.horizontal, 20) .frame(width: 80, height: 80, alignment: .center)
UPDTAE :
If I add TestApp in target membership of Assets.xcassets, Image is loading. (TestApp is in Framework project so it was feasible) But once this Framework is shipped to client they won't be able to access xcassets. client will just get .framework
Upvotes: 3
Views: 1611
Reputation: 155
Image loaded inside framework need to specify the Bundle corresponding to the Framework. This will enable Image load on client side.
Creating the extension reduced redundant code
extension Image {
init(imageName: String) {
self.init(imageName, bundle: Bundle(identifier:"com.company.framework"))
}
}
Update : Similarly if you are using Localisation below extension can be used :
extension Text {
init(textKey: LocalizedStringKey) {
self.init(textKey, bundle: Bundle(identifier:"com.company.framework"))
}
}
Upvotes: 1
Reputation: 159
Are you sure that you added the Image in xcassets? Make sure that the name is right.
Update: Perhaps this: Images inaccessible from asset catalog in a SwiftUI framework
Upvotes: 1