iOS UIImage returns null - ios

Why is this not working?
//set up check mark image
imgCheckMark = [UIImage imageNamed:#"checkmark.png"];
vImgCheckMark = [[UIImageView alloc] initWithImage:imgCheckMark];
NSLog(#"%#", imgCheckMark);
I keep getting null in the console. The image is in the bundle, I can view it in editor.

Remove the image Checkmark.png from the project
add it back and try it
Make sure that the image is in the project folder[while adding check
copy items into destination group (if needed)]
Make sure the image name is exactly the same as in the file

Check the filename for case sensitivity. If your file is Checkmark.png then it will not work properly on the device. This questions has a few more details:
Works on iPhone Simulator but not on device

Related

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.

How to change image with device language? Xcode

I have a background image that has some texts too. So I want to change it with the device language. But the background changes with if statement in my code. So I need to change the image itself to optimize it for another language. How can I make this happen?
You can do this by creating a UIImage with the "imageNamed" constructor, but use an NSLocalizedString for the image name:
UIImage* localizeImg = [UIImage imageNamed:NSLocalizedString(#"imageNameKey", nil)];
Then in your localized .strings files you just put the name of the image that you want tied to that language.
"imageNameKey" = "englishBackground.png";
Apply the newly created UIImage to the background and voila! Hope that helps!

Picture doesn't show on iPhone when running app

I have set the backgroundColor to a picture which is located in the project folder. When I run the app in the simulator the pic is shown. But when running on my real iPhone it's just white. What can be causing this? Could it be because I have <0.5gb free on both the computer and iPhone?
I had similar situation.
It seems the simulator - Xcode is cacheing the image somewhere, but at build time is loosing.
Try: uninstall the app from simulator, device.
Clean the project.
Force close XCode, not just the project, close it.
Open, clean project, build
At this step should match the device and simulator behaviour, hopefully it is there the desired image.
Please check spelling of the image name you used. In actual devices it's case sensitive.
That should be plenty of free memory. When adding the file to your project, and make sure you choose the options for the image to be copied into the project and to be part of the target.
EDIT: my above suggestion is only useful if the UIImageView you are trying to load points to a picture, and I guess in this case you are just using a blank UIImageView and setting background color.
Also, you might want to look at the arrangement (ie. send to back, send to front, etc. Try sending to front in case there was something in front of it blocking the view) I think these options are in the menu bar under Design --> Arrangement
Could you post some code?
You'll want to add the image not as a backgroundColor, but as a UIImageView subview.
// load the image
UIImage *image = [UIImage imageNamed:#"image.png"];
// create the image view using the image
UIImageView *imageView = [UIImageView alloc] initWithImage:image];
// add the image view as a subview
[yourview addSubview:imageView];
First try to Uninstall the app Clean the project then Install and then check if not then.
Second there may some issue with picture spelling Case senstive or Not As The iPhone is case sensitive, but the simulator is not because iPhone file system is case sensitive and Mac OS is not
Third
Add a break point where you add UIImage
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"a_image.png"];
Then on the console "po [imgView image]"
If it is nil then either the resource is not getting copied correctly into the bundle.

Custom button not appearing on iOS device

So I'm trying to implement a custom button in my iOS app to replace the default rounded rectangle button. Heres a code snippet:
UIImage *normalImage = [[UIImage imageNamed:#"Images/whitebutton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
UIImage *pressedImage = [[UIImage imageNamed:#"Images/bluebutton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[self.theButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[self.theButton setBackgroundImage:pressedImage forState:UIControlStateHighlighted];
When I run it in the simulator everything works fine, my custom button shows up. However, when I run it on my actual device the button just appears as the default one with no customisation at all. Anybody got any idea where I'm going wrong?
The iOS file system is case sensitive, unlike the sinulator's. I'd start by checking that "Images" directory.
(Quick test if I'm barking up the wrong tree: does the +imageNamed: call return nil?)
You should add whitebutton.png and bluebutton.png to your project. And call imageNamed:#"filename" not imageNamed:#"file path" . Also , when running on the simulator there is no problem with case sensitivity. On the device , you have to type the correct name of the file because it IS case sensitive.
Hope this helps.
Cheers,
George
Your resources are going to get flattened in your app bundle. Also, the filesystem on iOS is case sensitive while the filesystem on Mac OS X is case preserving, so make sure the actual filenames of your images are case-correct. If they are, just remove the Images/ prefix and it should work:
[UIImage imageNamed:#"bluebutton"];
Also, if the image is a png type, you don't need to specify the file extension with imageNamed:.
Use
[UIImage imageNamed:#"whitebutton.png"]
and
[UIImage imageNamed:#"bluebutton.png"]
Regardless of how you have it visually organized in Xcode, it all gets flattened in the bundle

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