In My Project, there is one framework project and this framework project has images in the assets catalog..
I want to use theses images using the storyboard of my main project.
I can use the images in class files by defining the bundle of the framework like this,
myImageView.image = UIImage(named: "img1", in: Bundle.init(identifier: "com.abc"), compatibleWith: nil)
but not in the storyboard. so Can I use it in storyboard or I have to go using code only ?
You cannot use assets of other frameworks directly, but you can add the assets to your main target as reference:
"Add Files to Project.xcodeproj..."
You will then be able to choose the images of the assets container in the attributes inspector of image views and buttons.
Please follow below steps to user framework image assets.
Create bundle resource for your framework project
Add your required images into that bundle resource
Build your framework and drag that framework into your project
Also drag and drop that bundle resource into your project from framework package.
Use any images into your storyboard which you added into the bundle resources.
Please refer below image for more details.
Related
With the new Swift 5.3 package manager, it's possible for a Swift package to vend out images from an Asset catalog. Let's say I have a package called "ImageLibrary" which has image assets I wish to share between many projects.
Now I have my main app project and I import the Swift package. In my storyboard, XCode lets me select images from the asset catalog in my library "ImageLibrary". However, at runtime the images cannot be found, because they exist in a different bundle to the storyboard.
The only solution I can find is to drag the Image asset library from the "ImageLibrary" Swift package into my "Copy Bundle Resources" list. And this does works. The images end up in the app's bundle and appear at runtime.
The problem is that if my Swift package is referred to with a remote repository link, XCode will check the code out and compile it deep inside the Derived Data folders. And the "Copy Bundle Resources" link will point to that folder. And now the XCode project file has a link with information specific to my hard-disk buried in it. With a large team that won't work of course.
I could possibly use a local Swift package to deal with this, but I anticipate similar problems with continuous integration tools and so on. I suspect this is just something that was not intended to be done with Swift packages, and that I'm out of luck.
Thanks for any ideas,
Simon.
One approach you can take is to declare public UIImages initialized with your graphics from the asset catalog all within the Swift package. Then the UIImage will be available to your main app project. This approach will work remote Swift packages as well as local Swift packages, so long as you are able to edit the Swift package source code yourself to update it.
I want to create a framework that includes xib.
Do I have to use the bundle?
If so, should I distribute the bundle file and the .framework file when I deploy?
The framework I created seems a bit crude.
Do you have any examples of frameworks including xib files?
Thank you for your reply. :)
Framework will automatically wrap your resource files (xcassets, images, storyboard, xib) into bundle inside the .framework file after compile, you dont really need to do anything.
Static library then you will have to create bundle target and pack it yourself.
In new Xcode 9 I want to add images in project. But If I add images and try to use it in code or in storyboard my images not showing in simulator. But in Xcode 8 all works fine. How to fix it?
Note : This Problem does not Exist Anymore in newer XCode
If you have dragged image in your project, you must select Copy If Needed While adding any file into your project.
After Adding Image take a look at your image if it is available to target, I have face similar issue with other file types too.
In xcode9 it was not selected by default even after selecting copy if needed.
Yes - this is super annoying. With XCode 9, now in addition to dragging a new image into the XCode Project Navigator (the left-hand list of files), you now have to go into Targets for your project, go to "Build Phases" and manually add the image file to "Copy Bundle Resources" using the "+" button at the bottom of the list of files.
This is required for any object you used to have to just drag into the project, including third-party frameworks and bundles.
PS: in dragging the image into your project you don't have to have "Copy Item if needed" selected if the image file is already somewhere in your project folder or a subfolder thereof.
If you have to handle multiple images, it is probably more convenient to copy the resources to your target app in the General Settings > Build Phases > Copy Bundle Resources
Copy Bundle Resources
This does the same as checking Target Membership for the resource – but you can add multiple images to your target app at once.
Check release notes of XCode 9.1:
Adding files to a project now adds them to the selected targets. (34551617)
https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html
I had two people discuss two different approaches with me on how to add assets such as images to a project in Swift.
Person 1: "You should create a new folder within your actual project workspace and then add all of your assets to it directly (so it's within the project). Then drag & drop the assets from this project folder to XCAssets, to actually create the icons you will use."
Person 2: "Just download your images from wherever, and drag and drop them directly. They can be from different locations. When you deploy to ITunesConnect it will copy the images."
Now I'm confused - when I drag and drop an image (let's say from my Downloads folder) doesn't Xcode copy the image locally into its own private location? Once I drag it into XCAssets, does it need that source location anymore? I thought maybe approach #1 would end up forcing Xcode to store duplicate images/assets and take up more size on the app.
What is the "correct" approach to adding assets?
Thanks!
Both actually can work (drag and drop/use asset folder). But the latest method introduced by Apple by xcasset folder. XCAsset folder is superior in the way that you can see a list of assets nicely one by one, even you have multiple sizes on it (.png #2x.png #3x.png etc). Easier to manage and see which size you are missing etc.
My issue is:
I have several projects/targets with shared xcassets.
What I want to do is to set images inside storyboard without code.
As I was able to find, there are no way to set image in storyboard from different bundle.
I created UIImageView extension, but don't think about like the best solution.
Is there exist a way to use assets in storyboard from different bundle?
Thanks.
So, based on answers to this question, I do know of one solution. Sounds like you've already done some of this, but just in the interest of giving a complete answer:
Create a framework project to house your shared images (it's great for shared code too!): File > New > Project > iOS > Cocoa Touch Framework.
Create a new Asset Catalogue within the framework File > New > File > iOS > Resource > Asset Catalogue.
Import your images into the Asset Catalogue.
Create a new workspace: File > New > Workspace.
Add both your app and the framework to the workspace: File > Add Files to WorkspaceName.
Include the framework as an 'Embedded Binary' within the app's project: Click the blue project file icon in the project navigator, go to the General project settings and locate Embedded binaries. Click + and select the Framework from the list (e.g. ExampleFramework.framework).
The above should be enough to allow you to use images from the framework's Asset Catalogue when they are called using code...
let frameworkBundle = Bundle(for: FrameWorkClass.self)
guard let image = UIImage(named: "MyImage", in: frameworkBundle, compatibileWith: nil) else { ... }
...however, to use it in a storyboard or XIB, there's one more step needed. Whilst the image names will now be selectable from Interface Builder (and even display there when selected), storyboards can only reference their own bundles, so it'll give you the following error when the app runs, and the image will be missing:
Could not load the "ImageName" image referenced from a nib in the
bundle with identifier "BundleName"
To fix this, you need to go to the Workspace's navigator (left panel) and drag the Asset Catalogue (i.e. the .xcassets file) from the framework to somewhere within the app project itself (e.g. the project's root folder).
In the dialog that appears, be sure that your app is checked in the Add to targets list, and that you leave the following option unchecked.
This will create a reference in your app's main bundle to the Asset Catalogue in the framework, but without creating a duplicate of it.
Once you've done that, you can use the images from that catalogue just like any other within your storyboards & XIBs.
Honestly, I can't vouch for if this is a best-practice, but it's the only way I've found to achieve what I also feel to be a sensible means of minimizing duplication between projects. In case you're interested, I've posted a question here to try and determine if this causes any duplication of the assets on build.