Access NSImage from Assets programmatically with IBDesignable - swift

The regular loading via NSImage(named: "image_name") crashes the IBDesignable renderer as it apparently runs in a separate bundle.
In iOS there is a workaround to load images passing in the bundle explicitly:
let bundle = Bundle(for: self.classForCoder)
let image = UIImage(named: "image_name", in: bundle, compatibleWith: self.traitCollection)!
In Cocoa the analog initializer is missing.
Is there any other way to load the images from the Assets catalog?

If you're using Swift 3 or higher, you can try image literals. It should auto-complete in Xcode so if it doesn't work it might indicate a problem with the asset itself. Using a UIButton for example:
let myButton = UIButton()
myButton.setImage(#imageLiteral(resourceName: "image_name"), for: .normal)
By typing Image Literal Xcode should auto-complete it and create a small icon which you can double-click to open an image picker and select the image you want to use. A more complete guide on image literals and literals in general can be found here

Related

Swift how to reference an image file that is not in *.xcassets folder in storyboard

I was trying to update the icon of my app in LaunchScreen, turned out that the UIImage doesn't update for the situation that a user update from old version to new version, and I searched on web for some solutions, there is one suggesting that I should load image files that are out of the *.xcassets folder, I know how to put the image files to my project root directory, but I don't know how to load it in storyboard, as LaunchScreen.storyboard can't have a view controller class
So my question is: How to reference image files in storyboard
This is the source link: http://arsenkin.com/launch_screen_image_cache.html
You can't use any custom behavior in LaunchScreen.storyboard. And loading images from anywhere except *.xcassets is a custom behavior.
Generally you'd use something like
let path = Bundle.main.path(forResource: "image", ofType: "imageType")
let image = UIImage(contentsOfFile: path!)
to load such an image, but as I've already mentioned, this approach doesn't work for launch screens

Load image to uiimageview based on the selected target in Xcode

I'm trying to create two applications from same codebase. These application will have same functionality but different logos, app name etc. I have created two targets with different bundle Ids so now I get unique apps when running each target.In my app, I have a viewcontroller with an UIImageView.
Now I want to assign an image to UIImageView based on the running target. So I created two assets folders.
Target1 -> Assest1 -> logo.png
Target2 -> Assest2 -> logo.png
logo.png is different file with same name.
How can I assign the correct logo.png to uiimageview based on the running target? Your help is appreciated.
Ensure that your asset is in the correct target an then use the following method:
UIImage(named: "example", in: Bundle(for: YourClass.self), with: nil)
To obtain your bundle you could use your class to refer to your bundle:
Bundle(for: YourClass.self)

Image names from assets catalog is not showing up in auto completion - xcode 12

I am trying to set UIColor from image pattern. But I am unable to access the image set by name in xcode 12.
I saved the image set by "BMW" name. I want If I write BMW it should pre-populate in suggestion and small image of BMW should shown up.
I tried to set like this -
node.geometry?.firstMaterial?.diffuse.contents = #imageLiteral(resourceName: "Earth Day")
But it is showing up as
node image
I got it now.
Although images names are still not pre populating, but we can pick the images from assets by typing #imageLiteral() and double click on it, one popup will open that lets you pick any image from assets.
Use UIImage(named:"BMW") to access the image from your assets folder

none of views showing image

not setting image on every views like UIView , UIImageView
mainView.backgroundColor = UIColor(patternImage: UIImage(named: "headerDriver.png")!)
img.image = UIImage(named: "about")
and there is warning in console
Driver was compiled with optimization - stepping may behave oddly; variables may not be available.
i have test it with image literal to make sure image exist
i suddenly deleted the optimization level and now drop down menu is gone i can't change the value of it
there is a problem with
optimization level
in build setting i'm sure
as you mentioned if asset folder is not in compile resource.xcode will not compile it so your app crash because there is no asset folder either all you pictures
solution :
navigate your target -> build phases - > compile source -> add (+)

Save an image from keyboard extension to clipboard to make it ready to be pasted in a textfield by the user in Swift

Can you please provide me with the code in swift for saving an image to clipboard and how it should be implemented.
If you have any related tutorial that would be great too.
Short example :
let image = UIImage(named: "image.png")
UIPasteboard.generalPasteboard().image = image;
For more information, look for UIPasteboard Class Reference : http://goo.gl/NaSt8z
PS : You might have to test that on device and not on simulator.