How To Load The Image Set(The Bundle) From Images.xcasset in iOS - 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"])

Related

What name should i give for a ipad background image

Im creating a universal app. in my login page i am having a background image i named it as LoginBG#2x and LoginBG#3x for iphone-4,5 and iphone-6 respectively. So, what name should i give for the ipad version images so that iOS will automatically pick them up when my app is viewed in iPad
Use Image Asset Catalogs instead.
It manages image according to device.
You can drag and drop images in respective position -
For more information read - Asset Catalogs
You can give the name as :
file~ipad.png
file#2x~ipad.png
Or batter you can use the assets catalog
Yo can give name like easily you can remember.
like
abc.png
abc_ipad.png

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.

Why does the Images.xcassets not accept my jpegs?

I'm following along with Paul Hegarty on iTunesU and he adds jpegs to his project by selecting a group in finder and dropping them into the view associated with Images.xcassets. In my project that view has white icons called AppIcon and LaunchIcon. In the tutorial the jpegs are dropped and added, then a simple [UIImage imageNamed: ... can load them into the running program. But when I try to drop images onto the area, nothing happens.
I tried adding them to the project tree view on the left, and they were accepted there, but didn't get [UIImage imageNamed: to work.
Any idea what is wrong here?
TIA
Mark
There is no where such written that we have to use only png for assets catalog, but only I have seen at Create and Set iOS Launch Images they are saying for png, why don't you convert your jpg to png and give a shot.

Naming Convention For Images in XCode 5

i am using XCode 5 when i am naming the images like that :
fbicon~ipad.png
fbicon~ipad#2x.png
Xib is getting the image
but when i am using this Convention
although tha naming convention is
fbicon~ipad.png
fbicon#2x~ipad.png
xib is not picking the images in that case.
Dont Know Why?
Migrate to use the assets catalogue. You just drag your images into the image wells and interface builder picks them up.
If the catalogue doesn't show the iPad iphone specific wells, open up the properties right side bar and tick the appropriate boxes.
assets catalogue
This is happening since you are using XIB.
When used with XIB, for images to be used on iPhone one must use the convention "image~iphone" and for iPad you have to use "image~ipad". #2x is automatically appended based on which version (retina/non-retina) of the image is needed. That's why the first convention works for you.
In case of getting images from the code, e.g. if you have following versions of the "image"-
image~iphone.png
image~ipad.png
image#2x~iphone.png
image#2x~ipad.png
You just call-
[UIImage imageNamed:#"image"]
In this case, the second convention that you mentioned works- as detailed in the Apple doc-
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html
So, nothing really wrong here as I see. For XIBs, the first convention is correct. For using with code, second convention is correct.

UIImage imageNamed returns nil

I am pretty new to iOS and Xcode.
I tried to load an image with
[UIImage imageNamed:#"imageName.png/jpg and so on"];
but it only returns nil. The image should be in my project bundle, considering the fact that I can choose it from drag and drop menus in the InterfaceBuilder and it was added to Xcode via the "Add files to "projectname"..." menu.
It makes no difference whether I use .png or .jpg images, the result stays the same: they work via IB but not if I try to run the imageNamed method myself.
There are only a few reasons why an image would come back nil with imageNamed:
The image is not in your bundle. Make sure the image is actually in your project. Make sure the target is checked by clicking on the file and selecting the target it belongs to.
You have the image name spelled incorrectly or a problem with the extension.
You are using a retina display but do not have an #2x image. Try changing your simulator to retina and see if it shows up.
If you are using an asset catalog make sure you have the correct devices selected in the attribute inspector.
Some tips:
If you are testing using simulator delete the app off of your simulator and clean your project, then re-run. If it still shows up it should show up on your phone (if it doesn't it's probably an issue with the case of the filename or the #2x version).
If you are testing on your phone and it doesn't show up, make sure you are using the same version of the simulator (if you have an iPhone 4/4s make sure to use the 4/4s simulator with retina).
One last thing according to this post: JPG image doesn't load with UIImage imageNamed There is some issue with certain JPG types working with imageNamed and no extension. IMO, you should be using PNGs anyway since iOS compresses them for you, unless you just have to use JPG.
Re Mike Weller's comment. The checkbox is ....
If you verified all of the above and are finding that your UIImage(named: "myImage") returns nil from code inside of a dynamic framework you are building, then you have to change the code to:
UIImage(named: "myImage", in: Bundle(identifier: "com.myframework.name"), compatibleWith: nil)
This should retrieve the asset from CXAssets correctly.
In my case, the PNG was malformed.
For some reason Xcode preview shown it correctly, but when I tried loading it with UIImage, it returned nil.
I just had this issue for jpg files named on disk "file.jpg"
I was trying to load without the extension, just #"file". While this works fine for pngs, it does not for jpg. Once I used #"file.jpg", it worked!
Swift 5
If you get UIImage nil in dynamic framework, you should set image with bundle identifier.
You can create an extension to Bundle like this;
Create an extension;
extension Bundle {
public static let myFramework = Bundle(identifier: "com.myFramework.bundle")
}
Then you can use like this;
UIImage(named: "myImageName", in: Bundle.myFramework, compatibleWith: nil)
Or
You can use like this;
UIImage(named: "myImageName", in: Bundle(for: type(of: self)), compatibleWith: nil)
I think the first one is more useful because it's more readable than second one.
Try re-exporting your image.
I had a similar problem, UIImage imageNamed was returning nil and none of these answers fixed my problem.
I found out that it was actually something wrong with the png file. I opened the file in GIMP image editor, saved it as a new file, exported it again as png and magically it started working.
I didn't change anything in code at all so there was definitely something wrong with the actual image file.
try
[UIImage imageNamed:#"imageName.png"];
instead (with an extension)
I had the same problem: #"photo.jpg" resulted in 'nil' UIImage.
Changing to the "actual" filespec, #"photo.JPG" works fine!
I hadn't realized there was case-sensitivity there! VERY non-intuitive.
i had a similar issue, finally the cleanup fixed it: the image showed up in the simulator, but id did not show up when running the app on the iPhone.
What I did:
1) deleted the app from the iPhone
2) performed Product/Clean
after that the bug was fixed and the image showed up!
Try to use UIImage(contentsOfFile:) instead of imageNamed. It worked for me with downloaded images.
Check that file has no space at the end.
The name like name #2x.png will fail loading, but name#2x.png is fine
Does the image work on the simulator but not on the device?
If so, in this scenario it is always to do with the case of the name. The device requires exact case and the simulator is not so fussy if I remember correctly.
e.g. for an image named image.png
[UIImage imageNamed:#"Image"];
would work on the simulator but not on the device...
Make sure you have the image for the DEVICE you are using in your .xcassets file. An image flagged for iPad won't show up on the phone (or the simulated phone).
In addition to #Inturbidus's answer:
For those who came here working on Application Extensions:
The image you are working with should belongs to both targets - hosted app and extension.
In other case you'll always getting nil trying to access it from within the extension's code.
For Xcode 6.4, click on Images.xcassets and check whether there are entries for each image that you called. You may right-click on the image and select "Show in Finder" to check if the images are the correct ones added.
Make sure the Attributes Inspector name field of the image matches exactly the name you're using. In my case the image was part of a folder and so the image name was "folder/name". Using this long name solved the problem. XCode 7.3.1
In my case, using a name with accented characters proved to be a poor idea. I changed Astéroïdes to Asteroids and it worked fine.
In my case problem was with image name in asset catalog.
For example if you have image with name "TEst" and change name to "Test", changes won't be indexed. If your teammate will pull commit with this changes, image in his asset catalog will have previous name "TEst".
Very small chance to catch this situation but it's possible.
I had multiple .xcassets in my projects and 2 had duplicate name of images. So it was not loading the image in my case.

Resources