Getting launch image from XCAssets using imageWithContentsOfFile: - ios

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.

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"])

How to store images and refere to it for my iOS interface?

I'm using XCode 6.
Let's say I have a Food model containing a (UIImage *)image property. I would like to display all my foods displaying images for my interface.
I have already the pictures for the foods (they are not in the Photo Library of the device but on my computer).
1) Where to put these image files (beet.png, banana.png etc... for example) in Xcode ? (is there a dedicated folder ?)
2) How to refere to this image file ? Example in my Food class, self.image = ?
Thanks a lot !
You can just drag and drop image files to your project while xcode is open.
make sure you use copy items if needed option, it is less hassle.
once you clikc finish for your files, you will see your files on bundle.
then all you need to do is call
[UIImage imageNamed:#"imageInBundle.png"];
imageNamed: will search your main bundle
or long way
NSBundle* bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle]URLForResource:#"YourBundle" withExtension:#"bundle"]];
NSString *imagePath = [bundle pathForResource:#"imageInBundle" ofType:#"png"];
UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
1) you can create new group inside your project in XCode and drag images to it. Check in build phases, in Copy Bundle Resources to make sure they'll be copyed in final app. 2) you refer image using imageWithName. You can use UIImageView for easier management and set this image your imageWithName. Otherwise you can implement directly images in your xib. Anyway how do you want to use images? In a table, in a collection, in a detail view? Depends on you how you want to display them and how you manage to make things work.

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