Adding image resource to iOS app and access it - ios

I have an Xcode project which needs to access an image. My questions are:
How to add images to the Xcode project. Do I need to follow a specific structure or convention?
How do I access the image I added in step (a)
Online links say add bundle resources but none of them gives a conclusive answer showing what to add in Xcode project and how to access the same.

The best way to save image in your folder by creating an Asset Folder.
File -> New File -> Asset Catalog
Name the asset as image, it will create an asset folder image.xcassets
In this asset catalog, you will keep your images in specific folder.
you have to just write
imageView.image = UIImage(named : "MY_IMAGE_NAME")

When you create a project,there would be a folder named Assets.xcassets.You can drag the image you need into the Assets.xcassets.
And when you need to show the image,code this:
//the image name is `image.jpg`
let image = UIImage(named : "image")
imageView.image = image
PS:
You can also drag the image to the project directly,and the code:
//the image name is `image.jpg`
let image = UIImage(named : "image.jpg")
imageView.image = image

You will be able to find a folder named Assets.xcassets in your project hierarchy.
Drag and drop the image you wish to add into this folder.Here you can select 1X 2X or 3X size for your image.
After doing this you will have to create an UIImage object to access it.
for example:
let image = UIImage(named:"image_name")
Now you can use this image object wherever you wish to use the image.

Whenever you are creating a project, you getting a folder Assets.xcassets. In this just add a New Image Set, add the name which one is your image name and drop here 1x, 2x & 3x here.
OR you can create a separate Asset Cataolog.

Related

Can't add UIImage to UIImageView (Swift)

I am trying to add a UIImage to a UIImageView in Swift as of March 2019. The image is titled correctly and is saved in the iCloud Drive in the same directory as the Swift file. I am able to add the UIImageView to a view controller perfectly fine but the image does not show up in it. I believe I have the right file name as I copied it directly from the finder. What I want to do is have an image inside of a viewer which is scaled by aspect ratio to fit the viewer.
let discoverIcon = UIImage(named: "DiscoverButtonIcon.png")
let discoverImageView = UIImageView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
discoverImageView.image = discoverIcon
discoverImageView.contentMode = .scaleAspectFit
view.addSubview(discoverImageView)
This is what I have tried to do but the UIImage will not show up.
Temporarily change
UIImage(named: "DiscoverButtonIcon.png")
To
UIImage(named: "DiscoverButtonIcon.png")!
Run the app. If it crashes, you have the wrong name or the image is not in your app bundle at all.
is saved in the iCloud Drive in the same directory as the Swift file
That sounds like the problem. That is not where the image needs to be. It needs to be in your asset catalog or app bundle.
The best practice would be adding the image to assets folder. By adding the image your assets.xcassets it will be added to your Bundle Resources.
If you don't want to add it to the assets.xcassets folder, add the image as another item in your project. However, make sure you add the image to the right target. (You can check in File Inspector > Target Membership)
You can check Build Phases > Copy Bundle Resources to make sure that you add the image to the Bundle correctly.
// If you add image to assets.xcassets
let image = UIImage(named: "DiscoverButtonIcon")
// If you add image as a resource
let image = UIImage(named: "DiscoverButtonIcon.png")
// Set a break point here to debug your image.
Related debugging post: Xcode debugging - displaying images
Try to add your image to Images.xcassets.

How to get image from assets by folder ios

I heve Assets catalog like this
Assets
ComponentA - folder
Background - image
ComponentB - folder
Background - image
how i can get image depend of component (folder) ?
I like to use something what it is
let image = UIImage(named:"ComponentA/Background")
but this doesn't work
you can get image depending on folder by "Providing Namespace" to folder.
Select folder in .xcassets
Check "Providing Namespace" in last tab (see attached image)
Access image using var image : UIImage = UIImage(named:"New Folder/bgimg")
folders are for our reference only
you need to provide different names, and use only image names in programming.

All images in xcassets, then how load the image referenced from a nib in my bundle

In my iOS project, I creat a xcasset named:PaoPaoImages.xcassets
then I put all images into PaoPaoImages.xcassets.
And I have a xib file named:a.xib, I compile it into a.nib.
Then I put a.nib into a bundle named:paopao.bundle.
So, now, my images in PaoPaoImages.xcassets , and my nib in PaoPao.bundle. And the nib reference the images.
Then, I run my project, the nib cannot show the images.
I'm stumped... any advice?
In order to access the image from the Asset Catalog, you only need to access the name of the asset group without any extensions.
So, if you add an image named #"my-button#2x.png" to the Asset Catalog, it will create an asset group called my-button.
Now, all you have to do is access the image like so:
In coding use following line
[UIImage imageNamed:#"my-button"];
instead of
[UIImage imageNamed:#"my-button.png"];
Also, you can edit the asset group by renaming it (without renaming the images) or changing it's individual components. This will allow you to follow easier naming conventions as well as show completely different assets between different UIScreen scales without any scale checks.
In order to incorporate images for different device sizes, you may need to toggle it under the "Devices" subheading in the Asset Catalog Group's options.
==> check here code try it:-
NSBundle *myImageBundle=[NSBundle bundleWithPath: myBundlePath];
NSArray *ImageURLs=[myImageBundleURLsForResourcesWithExtension:#"png" subdirectory:nil];
NSLog(#"imageURLS: %#",ImageURLs);
=>here refrence more try it:-
Access Asset Catalog programmatically
Load image from bundle with IOS

iOS App Issue - Can't access image from image asset catalog

I'm new to iOS development, so apologies for advance. I know this has been covered earlier but I can't seem to find anything that helps with my issue.
Its a fairly simple but annoying issue.
I have a image in assets with a file name of hollwood.png but a xcode name, "hollywood" (no png).
I am trying to set a UIImageView to display it.
So in ViewDidLoad I have the following: (imageContainer is the UIImageView IBOutlet)
- (void)viewDidLoad {
super viewDidLoad];
self.model = [Model sharedModel];
self.imageContainer.image = [UIImage imageNamed:#"hollywood"];
}
I can not get it to display the image from Assets.
I know the UIImageView outlet works correctly because I can have the app choose a photo from gallery and set the photo to UIImageView.
I'm having a very difficult time figuring out how to load the image I saved in the assets catalog into UIImageView.
I just had this issue. I had dragged the image and for some reason Xcode added a weird "Design" locale and I couldn't load the image using UIImage(named: ). I don't know if this is your case, but you can try right clicking on the image in the Asset Catalog, "Show in Finder", opening the folder and checking the "Contents.json" file. Each image there should only have the keys "idiom", "filename" and "scale".
Make sure your image is added in Compile Sources
AppTarget->Build Phases-> Compile Sources
If not try to add that and check.

How to use Image assets in iOS

I would like to use the launch images from the xcasset; I've already tried some ways but I can't get it works.
I've the standard LaunchImage asset, and the image file are called Default[#2x,...]. Using [UIImage imageNamed:#"LaunchImage"]; always return nil. I've also tried with #"Default", but no results.
If you Just want to use the compiled images separately in your application:
By-default LaunchImage asset would generate following files:
LaunchImage-700-Landscape#2x~ipad.png
LaunchImage-700-Landscape~ipad.png
LaunchImage-700-Portrait#2x~ipad.png
LaunchImage-700-Portrait~ipad.png
To find them use below:
UIImage* image = [UIImage imageNamed:#"LaunchImage-700-Portrait"];
Note:
3 steps required to setup the assets properly are mentioned below. Are you missing anything ?
My Images:
Updating images in Launch Image source:
Drag and drop images in LaunchImage asset:

Resources