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

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

Related

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.

How To Load The Image Set(The Bundle) From Images.xcasset in iOS

When I am developing apps in iOS before, I always put the image sources of the app at the root directory of the project, and then I will load them using [UIImage imageNamed:#"image.png"]. I did't even include various sizes and resolutions such as image#2x or image#3x. But I am afraid this is not a good practice, because I cannot deal with different screen resolutions.
Now, I want to make use of the Images.xcasset to store all my image sources so that I will be able to load just the bundle name, and hoping that the system will pick the image with the right resolution automatically. Therefore, I made a test and place my image set under Images.xcasset and named it as images, and then in my UIViewController.m I am trying to load the image by calling [UIImage imageName:images]. As a result, it didn't work. So I searched for an answer and found out that I should call [UIImage imageName:#"images60x60#2x.png"] in order to load the the 60pt #2x image.
But I think this still did not solve my problem, because I am still choosing which image to load. Is there a way to load the entire image set or load the image according to the resolution of the screen?
Please help. Many Thanks.
EDIT: Added Screen Shots
I use AppIcon as a test
As you said at last that you are using AppIcon as a test. Please don't AppIcon it is made for internal use. try to create you own imageSet and use that. It should work.
Two things :
As said Ankit, do not use App Icon, use your own set
do not use a file suffix. Just reference [UIImage imageName:#"MyImage"] (not [UIImage imageName:#"MyImage.png"])

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.

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.

IOS Changing Main Bundle preference in XIB

I have a custom Resource bundle where i'll have all my images and localization files inside it.
When I assign some images in xib I have to type the entire path where image resides inside my bundle for example MyResource.bundle/Images/Event/xxx.png.
Which means in xib it always taking apps main bundle for images.
Here my question is is there any possibility is there like I can change the default bundle preference to MyResource.bundle/Images So that I can see the image in xib when assigning to a uiimageView or any other.
preference? in simple words its not possible because there is no property like that, so you need to find out a way to achieve this, but i have used bundle for resources but not have this kinda problem because bundle is logical path
My solution is add one image manager to manage your image resource. I mean when you need an image, you just call your manager's method passing in the image name. And your image manager will read the correct image file from corresponding bundle and return it based on device's language.

Resources