How to get image from assets by folder ios - 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.

Related

Adding image resource to iOS app and access it

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.

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

Image Assets: Could not load the image referenced from a nib in the bundle with identifier

I have a compass.jpg file that exists in Download folder. Then I dragged this image to my Images.xcassets. I made sure that the image is indeed within the folder by right click the image entry and view folder.
Then, in storyboard, I set my logo image view to this image, and with name compass. It all displays fine in the storyboard.
But, then, I started my app in my iphone, as well as in simulator. It gives me the following error:
Could not load the "compass" image referenced from a nib in the
bundle with identifier "com.x.Point"
I can't figure out how to solve this problem. Image's folder is indeed inside Images.xcassets folder.
How do I resolve this problem?
Update
I eventually have to make compromise to add the image into the project itself rather than its .imageassets folder.
I actually created a new project and copied and pasted all of my source code there, then .imageasset folder works again! I guess it is not a problem of my project settings. It must have something to do with XCode, I am still unsure of.
You need to drag the image outside of the Image.xcassets folder and into your project. The xcassets file is only for splash images and icons.
Have you tried setting the name to compass.jpg? I had a similar problem where the .jpg file from xcassets was not be loaded with the message Could not load the "xxx" image referenced from a nib in the bundle with identifier xxx, although it was correctly shown in the storyboard.
Adding the extension .jpg to the name resulted in the image not shown correctly in the storyboard preview, but loading correctly at runtime.

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:

PNG files are not displayed on IOS device after rename them

I had images starting with capital letters, I needed to change first letter of images manually on bundle , after doing that device can not show SOME of the images are not displayed while some of the images are displayed.
Such as:
C_120x120#2x.png
to
c_120x120#2x.png
I get an array of letters
legend (
g
)
and display them with
if ([testArray count]==1) {
NSString *legendName=[NSString stringWithFormat:#"%#_120x120#2x.png",[testArray objectAtIndex:0]];
UIImage *trackImage = [UIImage imageNamed:legendName];
cell.imageView.image= trackImage;
}
NSLOG:
track image string g_120x120#2x.png
track image (null)
In Bundle:
In finder project folder:
What I tried:
1) checked the case like above it looks like file names are correct;
2) deleted the images, changed the names before I add them to project.
3) delete the app on device , reinstalled it.
4) changed setting "Compress PNG Files" to NO
after doing everything above if I call C_120x120#2x.png in image it is displayed while c_120x120#2x.png is not displayed.
How can I solve this?
Try cleaning your project - Shift + Apple + K.
Try removing #2x and the file extension .png:
NSString *legendName=[NSString stringWithFormat:#"%#_120x120",[testArray objectAtIndex:0]];
It'll detect whether to use the #2x based on the device being used.
Also be sure that they're added to your bundle. Click on the image, and in the inspector look under 'Target Membership'. Make sure the appropriate target is checked. (see https://stackoverflow.com/a/10998746/1710611 for screenshot)
Make sure that the images are located in your "Copy Bundle Resources" section, it can be found in your project settings, select your target, and then it's under "Build Phases"
One of those PNGs was JPG and you just change the .jpg extension to .png
Good Luck

Resources