iOS: tab item image not loading - ios

I want to change the image of my tab bar item using self.tabBarItem.image = [UIImage imageNamed:#"home_icon.png"]; Seems simple enough, but I can't get it to run. Note: I'm simply replacing the file name from first to home_icon. The code works with the name "first".
Details:
I have started with the standard tabbed application and have only changed this one line of code. Xcode then loads the 64x64 image. I placed a home_icon.png file in the project and made sure it's a 30x30 png file. The docs says: "This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object." Why can't I simple reference a different file here? I don't understand what is actually getting referenced here. The original line is self.tabBarItem.image = [UIImage imageNamed:#"first"];, to reference first.png.
What I tried (see answers so far):
using home_icon, instead of home_icon.png
Using UIImage *img = [UIImage imageNamed:#"home.png"];
self.tabBarItem.image = img;
Adding the 2x file
Checking output console. No errors mentioned. (The app still references the old file successfully).
Checking target. File is added to target

Remove the ".png" from the end.

you should try below code;
UIImage *img = [UIImage imageNamed:#"image.png"];
[[tabBarController.viewControllers objectAtIndex:tabImageIndex] tabBarItem].image=img;

Use the Following Code For Changing The Background Of Tabbar. You can Add different different images According to your Tab.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControllers{
NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewControllers];
switch (index) {
case 0:
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_act21.png"]];
break;
case 1:
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_act22.png"]];
break;
case 2:
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_act23.png"]];
break;
default:
break;
}
return YES;
}
if you want to Change only Tabbar item Image Then Use following Code.
self.tabbarcontroller.tabBarItem.image=[UIImage imageNamed:#"Someimage.png";

Are you sure that [UIImage imageNamed:#"home.png"] return not nil? Check in console
NSLog(#"%#", [UIImage imageNamed:#"home.png"]);
If so, try to add the image to the Build Phases -> Copy Bundle Resources

The solution was to use only alpha channels in the png file. See http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html for details.

Related

Loading a UIImage from xcassets

I'm trying to load a launch image from the Image.xcassets folder but to no avail. There are other answers (and this one) on SO that purport to answer it but their main solution, to simply load an image like so
UIImage *image = [UIImage imageNamed:#"Default#2x"];
returns nil for me.
The filename is named correctly and the project is setup to use the assets.
Does anyone have any idea how I do this or what I could be doing wrong?
Thanks in advance.
EDIT:
EDIT 2: my final code:
-(void) loadSplashImage{
if ([self isiPad]){
self.imageViewSplash.image = [UIImage imageNamed:#"Default-Portrait"];
}
else{
if (self.view.frame.size.height == 480){
self.imageViewSplash.image = [UIImage imageNamed:#"Default"];
}
else if (self.view.frame.size.height == 568){
self.imageViewSplash.image = [UIImage imageNamed:#"Default-568h"];
}
else if (self.view.frame.size.height == 667){
self.imageViewSplash.image = [UIImage imageNamed:#"Default-667h"];
}
}
}
Please note it works for Portrait only.
You dont have to specify the size of the image in your name. It will automatically load the size that best fits for the device that runs the app. So your code should be.
UIImage *image = [UIImage imageNamed:#"Default"];
where default is the name of the resource from xcassets, the one you see on the leftside list.
Here's the way to get LaunchImage name.
Xcode 5 & Asset Catalog: How to reference the LaunchImage?
Getting image name from info.plist ([[NSBundle mainBundle] infoDictionary])
You should have 2 files:
(1) Default.png (or any other image format) — Non retina
(2) Default#2x.png — Retina
Now, to get this image, you will not have to use #2x at the end of the file name. Use just name of that image asset.

Error: CUICatalog: Invalid asset name supplied: (null), or invalid scale factor : 2.000000

TableViewApplication[1458:70b] CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000
Getting this warning while working with TableViewController. How to rectify this error and which block is affected?
This one appears when someone is trying to put nil in [UIImage imageNamed:]
Add symbolic breakpoint for [UIImage imageNamed:]
Add $arg3 == nil condition on Simulator, $r0 == nil condition on 32-bit iPhone, or $x2 == nil on 64-bit iPhone.
Run your application and see where debugger will stop.
P.S. Keep in mind this also happens if image name is empty string. You can check this by adding [(NSString*)$x2 length] == 0 to the condition.
This error (usually) happens when you try to load an image with [UIImage imageNamed:myImage] but iOS is not sure if myImage is really a NSString and then you have this warning.
You can fix this using:
[UIImage imageNamed:[NSString stringWithFormat:#"%#", myImage]]
Or you can simply check for the length of the name of the UIImage:
if (myImage && [myImage length]) {
[UIImage imageNamed:myImage];
}
Since the error is complaining that the name you gave is (null), this is most likely caused by calling [UIImage imageNamed:nil]. Or more specifically, passing in a variable hasn't been set, so it's equal to nil. While using stringWithFormat: would get rid of the error, I think there's a good chance it's not actually doing what you want. If the name you supply is a nil value, then using stringWithFormat: would result in it looking for an image that is literally named "(null)", as if you were calling [UIImage imageNamed:#"(null)"].
Something like this is probably a better option:
if (name) {
UIImage *image = [UIImage imageNamed:name];
} else {
// Do something else
}
You might want to set a breakpoint in Xcode on that "Do something else" line, to help you figure out why this code is getting called with a nil value in the first place.
In Xcode 6.4, this seems to occur when using "Selected Image" for a tab bar item in the storyboard, even if it's a valid image.
This doesn't actually seem to set the selected state image anyway, so it needs to be defined in User Defined Runtime Attributes, and removed from the SelectedImage attribute of the Tab Bar Item
In my case i was passing [UIImage imageNamed:#""] which caused me to show the warning. Just add breakpoints to all the lines where you have used imageNamed and the debug the line where you find the warning.
This happened to me after a storyboard was split into several (the actual change happened when I was on holidays, so I don't know exactly how it was done).
After inspecting the XML of the storyboards, I found that an image reference which previously had pointed to "bottomBar" in the assets catalogue instead pointed to imageView:fFo-1g-jzs:image.
At the end of the XML file under the <resources> tag was tag named <image name="imageView:fFo-1g-jzs:image"> containing a big mutableData blob.
After resetting the image reference in the storyboard and removing the blob, the error went away.
You are supplying some invalid image name, so need to validate before putting it you can do any of the above ways whichever make sense for your code or like
if (iconImageName && [iconImageName length])
{
[UIImage imageNamed:iconImageName];
}
else
{
iconImageName.hidden = YES;
}
Hope it will help!!!!
I have just fixed this error. Just check the usage of the [UIImage imageNamed:(NSString*) imageName] function. If the imageName is nil, then error occurs.
The Reason to the error is passing a nil value to the "imageNamed:" method. To avoid this, you can hide your imageView when you try to pass the nil value. The chances may occur in reusing the imageViews in UITableView or may be in scrollViews.
I avoided the warning with the below check :
UIImageView *your_image_view;
NSString *imageName;
if(imageName && imageName.length){
your_image_view.hidden = NO;
your_image_view.image = [UIImage imageNamed:imageName];
}
else {
your_image_view.hidden = YES;
}
I got this warning when I load image use [[UIImage imageNamed:normalStr] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal], than, I change to [UIImage imageNamed:normalStr], warning was gone.
One approach is do class method swizzling to replace [UIImage imageNamed:] with your own implementation, then check the image name in your implementation. See the following:
How to swizzle a class method on iOS?
I implemented this in my UIImage(Debug) category:
+ (UIImage *)db_imageNamed:(NSString *)imageName {
if ([imageName length] == 0) {
NSLog(#"breakpoint here");
}
return [self db_imageNamed:imageName]; // not a recursive call here after swizzling
}
You might want to swizzle [UIImage imageNamed:inBundle:compatibleWithTraitCollection:] as well.
In my case I have implemented cell with UILabel, UIImageView and UITextFied, sometimes I don't want to display image, in that case I'm sending empty string "" to the imageView. Now the same error/warning is getting.
Error: [framework] CUICatalog: Invalid asset name supplied: ''
Solution is we have to pass nil instead of "" empty string.
Code:
if imgName.count > 0 {
selectedIconView.image = UIImage(named: imgName)
} else {
selectedIconView.image = nil
//You can also use
//selectedIconView.isHidden = true
}
Maybe you can use this to confirm the "imageName" is not a "nil";Then you will leave away this warning.
if (imageName) {
self.imageView.image = [UIImage imageNamed:imageName];
}

UIImage not displayed

I am using iOS 7 SDK and Xcode 5. I want to set icons on UITabBarItem. I am using below method.
setFinishedSelectedImage: withFinishedUnselectedImage:
It was working till now. But suddenly it stopped displaying images.
I tried below various options :
[img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem setSelectedImage:img];
[tabBarItem setImage:img];
[[UITabBarItem alloc] initWithTitle:title image:img selectedImage:img];
None of them is working. Here, img is the image downloaded from URL.
Then I tried with one of the images in app resource and it was displaying.
After that, I tried using temporary UIImageView with background color to red and set img to this UIImageView, it displayed red color instead of img.
But strange part is that I use the same img in other view controllers where it is displayed correctly.
Any idea how to solve this issue?
It was my mistake in code. I had to initialze img to some UIImage on declaration.
Below is shown what was the issue.
UIImage * img; // Image not initialized
// This condition was not satisfied and hence `img` was not storing any time.
if (condition)
{
img = ...;
}
// Below condition should have been satisfied if `img` was not initialized.
// But it did not. I am not getting why.
if (!img)
{
NSLog(#"Image is nil");
}
[tabBarItem setImage:img];

Change Back And Forth UIImages

Hi I’m trying to get an image to change on a method call and if the method is recalled the original image comes back
-(void)change
{
if ((player.image = [UIImage imageNamed:#"first.png"]))
{
player.image = [UIImage imageNamed:#"second.png"]));
}
else
{
player.image = [UIImage imageNamed:#"first.png"]));
}
}
This works change the "first.png" image to "second.png" but when called again it doesn’t.
Where am I going wrong?
As pointed out by Michael Dautermann, the way you're comparing two UIImages is wrong. Can't you simply keep an NSString property that tells you the image you're showing?
-(void)change
{
if ([self.displayedImageTitle isEqualToString:#"first.png"]){
player.image = [UIImage imageNamed:#"second.png"]));
self.displayedImageTitle = #"second.png";
}
else{
player.image = [UIImage imageNamed:#"first.png"]));
self.displayedImageTitle = #"first.png";
}
}
In your code above, you're making an invalid assumption that "[UIImage imageNamed: #"first.png"]" will always be the same UIImage object when you do a comparison. That isn't true. Every time you call the "imageNamed" API, you may be creating a brand new UIImage object.
Now, since UIImage object doesn't know anything about file names or image titles, you need to keep track of which image is being displayed and then toggle on that.
I'd suggest creating a subclassed UIImage object that has a filename or title property (that you would also need to set), or have your two UIImages as properties or ivars loaded into memory (that is, if these aren't giant memory hogging images) and then do your toggling.

Can not load UIImage on button

I want to add UIImage on button,but the del.png can't show on my button,
What can i do?
I try many load Image Method,but no effect,such as:
UIImage* delImage = [[UIImage alloc] initWithContentsOfFile:#"del.png"];
UIImage* delImage = [UIImage imageNamed:#"del.png"];
UIImage* delImage =[UIImage imageWithContentsOfFile:#"del.png"];
CalDelBtn *BookItem = [[CalDelBtn alloc] initWithFrame:CGRectMake(startPointX, startPointY, 75, 113) statuePicture:delImage];
Please give me some advice,thank you!!
First, you image could be loaded with
UIImage * image = [UIImage imageNamed:#"del.png"];
This is the simplest way to load an image, so it should work with an autoreleased image.
Don't forget to add it to your xCode projet explorer! Here's a list of reasons for what your image generally woudn't display : https://stackoverflow.com/a/9698764/127493.
Then, add it to your button with :
[button setBackgroundImage:image forState:UIControlStateNormal];
for example. Does it work ?
Can you verify that your image is being added to your target? Xcode 4.3 has a nasty habit of not including files into targets when you drag and drop. So click on your project file on the left side of Xcode, go to Build Phases and see if your image is in the list under Copy Bundle Resources.

Resources