Storyboard, UIImageView. Load image from bundle - ios

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.

Related

How to set image in storyboard from framework assests

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.

Using XCAssets Properly (adding assets)

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.

Xcode -using two xcassets how would i override

Let me explain the architecture i'd like to build in xCode. here is a photo of xcassets i have created in the project structure:
From the image you can see that i have created two xcassets folders. The first one called images.xcassets hosts all images that are common to both my targets.Its membership is in both target1 and target2. This xcassets sheet is filled with many images which i inherited from a fork.
The images-override.xcassets folder on the other hand, is where i'd like to put any images that ARE NOT common to both my targets. So the target membership for this directory should be only target2.
What im trying to accomplish:
Doing it this way, if i attempt to retrieve an image called myImage.png it should first check the images-override to see if any image is overriden, if its not found there it should then check images.xcassets for the image.
So my question is about what order the images will get searched for. I'd like the project to first check the images-overriden.xcassets sheet then check (if necessary) the images.xcassets folder. Any ideas ?
In general, you cannot include multiple assets with the same name in Xcode. For your purpose, I'd recommend a naming convention:
filename.png
filename-override.png
For example. When loading your image, you first check if filename-override.png exists, if so load it. If not, load filename.png.

xCode won't list storyboard or info.plist for localization

I'm trying to localize an iOS app. According to all the tutorials, one of the first steps is to add localizations to the info pane of the project editor. When I get there, "use base internationalization" isn't checked by default. When I click it, the window "choose files and reference language..." comes up as expected, but it doesn't list any files--i.e. there are no choices for files to localize.
The app builds/runs fine. My storyboard is named "main.storyboard" and is in the project's main folder, so I'm not sure why xCode wouldn't recognize the file.
Of note, the project has gone through a re-naming, and I've been through a couple abortive attempts to localize after adding a language without the base, with the idea of adding the base later. But it looks like that takes me off track, and I had to undo those attempts; in the process, I manually deleted the .lproj folders that had been created.
Any ideas on what the problem is?
Important: Before you do any actions, keep backup of your project folder.
When you select use base internationalization, Xcode will look for User Interface files (Storyboard, Xibs) inside any *.lproj folders in your project. But in your case it won't find any *.lproj folder.
Steps to fix:
Create a "en.lproj" folder next to the storyboard using the Finder.
Then move one UI file like the main.storyboard (or other UI file) into the new folder.
On Xcode, relocate the storyboard file. One way to do it is to remove the old reference to the storyboard file. (will be in red) and re-add it to Xcode.
Now you will see that this file has "English" localization selected.
Retry now the 'use base internationalization' button and it should suggest you to move the main.storyboard to the Base.lproj which it will create for you.

Managing visual assets on xcode (reference, copy or both)?

Good evening overflowers,
I was wondering what would be the best way to manage assets (visual, sound, etc.) in xcode for ios or mac os projects.
I am currently working on a project that the image files change all the time (design is under construction still) and I would like to know the best way to keep all the files updated while having them in the project's directory.
So for instance I have a file named "ic_action_gray_button#2x.png" This button changes on the design but still has the same name. Is there a magical way to mirror the changes and have it also under source control without spending time finding and replacing the file all the time?
It is something easy when it comes to 50 assets but when these assets become 100 or 500 then, it becomes difficult to keep up with the changes.
Any tutorial or any guide to help me through this process?
If my question is not clear, be easy on me, I am not a native English speaker :)
Kind Regards,
Sonic555gr
You can create a separate folder for your assets inside your project folder. Then you can drag your asset folder to your project in Xcode, select "Create folder references for any added folders" and uncheck "Copy items into destination group's folder (if needed)". This should serve your purpose.
The way I do it is just to take the 100 - 150 or however many assets and paste them in Finder.
As long as the files have the same names, you are just doing an overwrite in Finder, inside the folder of your XCode project.
Any new images, you can add to XCode as usual, by drag & drop, then selecting copy and choosing the target. Any replacements can be done in Finder after that.
Git will also detect the file modifications and you can push the changes to your repo. For file/assets replacements, there is no need to do it through XCode and add the new images again.
If you are using asset catalogs for your images, its just a matter of replacing the files inside the specific folders, which is tedious, its then easier to just select them all inside XCode, delete them from the catalog, and from the project, and then just drag and drop them into the asset catalog again.

Resources