iOS App Issue - Can't access image from image asset catalog - ios

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.

Related

Image suggestions are not working Xcode 10

I am trying to set image to UIImageView. Its working well in older version.
In Xcode 9.4.1
But in Xcode 10
When I am trying to type name of image, image suggestion is not showing.
Even "Image Literal" is not working. Double clicking on above icon also not working.
Is there any setting in Xcode preferences?
How to enable image suggestion like before?
In Xcode 10 and Swift 4.2, only the code completion function (or auto-complete) of the Xcode IDE has been discontinued for the old way. Here is the new way:
Just type image literal and it will complete with default icon.
Double click on this icon and it will open the media library right side of it.
Just choose your image and it work like before!
Hope I am able to help you!
https://download.developer.apple.com/Developer_Tools/Xcode_10_beta_3/Release_Notes_for_Xcode_10_beta_3.pdf
Code Completion for Swift image literals has been removed in Xcode 10. (38087260)
There seem to be some real issues with how Xcode 10 handles image literals, especially when the name you provide your assets doesn't align with the filename of those assets.
The following are the steps I've found will restore image literal previews.
Add an image via the media library picker.
If you have multiple resolutions for your image (e.g. #1x, #2x, #3x), Xcode will inconveniently drop them all into your code and throw an error to let you know you've done something naughty.
Delete two of the faulty image previews, if necessary. You may be left with a default image icon instead of a preview of your actual image.
Comment out the lines on which you're setting the image. You'll see the image literal references the filename of your asset rather than the name you've provided in the asset catalog.
If you've renamed your assets after adding them to your asset catalog, this seems to sometimes prevent Xcode from reading the asset appropriately.
Change the filenames to the appropriate asset names.
You can find the asset names in your asset catalog.
Uncomment the code and, if the default image icon is still visible, double click on the image icon. It should then display a preview of your actual asset.
You may also need to clean and rebuild your project.
use command + shift + M to popup the Media Library then Simply Drag and Drop into your Code
OR

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

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.

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