How to use Image assets in iOS - 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:

Related

Not getting stored UIImage from WatchApp Target

I have added images in image assets in watchApp target and now I am trying to Load an array with these images. But every time I am getting image as nil.
ex. added A1.png, A2.png, A3.png......in image asset in watchApp target.
Now, trying to get image with image name as...
UIImage *img = [UIImage imageNamed:#"A1"];
But, every time I am getting nil.
Issue is that you have added the images in your WatchApp target and trying to load the image through an API which will search the images in your WatchExtention bundle. To understand this we need to see how images are being searched in bundles for WatchApp and WatchExtention.
Let's take an example that we have an object of WKInterfaceImage say objImage.
WKInterfaceImage *objImage;
When you use the method [objImage setImageNamed:#"abc"], system send the name of image to WatchApp and search for the image in WatchApp bundle not in the Extention bundle.
However when you specify your images in Extention target(bundle) then to use these images they need to be transferred to WatchApp target using imageNamed: method, which will search the image in Extention bundle not in WatchApp bundle.
So if you want to use the imageNamed: API, you need to specify your images in Extention target as well. However this has some overhead of it's own and i suggest not to use unless necessary.
Check the Apple Documentation:Section - Using Named Images to Improve Performance for detailed reference.

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

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.

Getting launch image from XCAssets using imageWithContentsOfFile:

I'm attempting to load images from my LaunchImage image set in a .xcassets file, but I don't want do use imageNamed:, as it wastes memory and the image only needs to be displayed for a couple of seconds during initial launch.
I have tried multiple approaches to this, but so far I have only been able to load them using imageNamed:.
This works:
[UIImage imageNamed:#"LaunchImage-700-568h.png"]
This doesn't work (returns null):
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"LaunchImage-700-568h" ofType:#"png"]]
Is there any way to do this without adding the resources explicitly to the target (aside from the assets file)? Thanks!
Looks like the answer to your question is no.
According to XCAssets documentation.
Each set in an asset catalog has a name. You can use that name to programmatically load any individual image contained in the set. To load an image, call the platform specific class method, passing in the name of the set that contains the image. The OS will load the image from the set that is most appropriate for the current scale factor. The platform method for iOS is imageNamed:. For OS X the platform method is imageNamed:
So we must use [UIImage imageNamed:] method to load images from XCAssets catalog on iOS.
See similar question and answers here.

Resources