Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting button image method

I want this method to check if a condition is true then set a specific button(button) to have the images I specify. Here is the code.

-(void)canAfford:(float) f: (UIButton*)button {
    if([self playerHas:(f)] == YES) {
        [button setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ButtonBGclicked.png"] forState:UIControlStateHighlighted];
    } else {
        [button setImage:[UIImage imageNamed:@"ButtonBG.png"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:nil] forState:UIControlStateHighlighted];
    }
}

Here is the error I get.

2013-09-22 10:35:39.985 Tapple[15663:a0b] CUICatalog: Invalid asset name supplied: , or invalid scale factor: 1.000000
like image 493
SHERRIE CRANE Avatar asked Sep 22 '13 09:09

SHERRIE CRANE


3 Answers

You are calling [UIImage imageNamed:@""] or [UIImage imageNamed:nil]. No image will match the empty string. If you are trying to clear the image, pass nil instead of calling imageNamed:.

Try this (updated original code, incorporating @Kyle Fang's comment):

- (void)canAfford:(float) f: (UIButton*)button {
    if([self playerHas:(f)] == YES) {
        [button setImage:nil forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ButtonBGclicked.png"] forState:UIControlStateHighlighted];
    } else {
        [button setImage:[UIImage imageNamed:@"ButtonBG.png"] forState:UIControlStateNormal];
        [button setImage:nil forState:UIControlStateHighlighted];
    } }
like image 63
bneely Avatar answered Oct 16 '22 14:10

bneely


A better way to use breakpoints to find the particular call that's causing +[UIImage imagenamed:] to generate the error is to use a Symbolic Breakpoint.

Open the Breakpoint Navigator and click on the "+" button at bottom left. Select "Add Symbolic Breakpoint...". Then edit the breakpoint to have these settings:

Symbol: +[UIImage imageNamed:] Condition: $r2 == nil

The Condition tests register r2, which points to the argument to the imageNamed: method, for a nil- valued argument to the method. You could also set the Condition to be (BOOL)[(NSString*)$r2 isEqualToString:@""], if you wanted to see if an empty string was being used as the argument. Note that the register r2 is present only in the case of debugging on a device. The simulator will use a differently named register as it's running on the x86 architecture.

When the conditions are satisfied, the debugger will break in the assembler code of the UIKit library, but you can locate the originating source of the call in the thread's stack trace.

You need only this single breakpoint to check your entire codebase for the source of the call.

like image 25
rfox Avatar answered Oct 16 '22 14:10

rfox


I started having the same error "CUICatalog: Invalid asset name supplied: , or invalid scale factor: 2.000000" after using the asset catalog, new in Xcode 5.

After some debugging I discovered that what was causing that error was passing an argument of "nil" or an empty string @"" to the class method [UIImage imageNamed:]. Even passing a file name that did not exist in the bundle did not trigger that error.

So I agree with bneely (hence got my up vote) for the fix. Now if you did what bneely suggested and still getting the error, make sure you go through your code and perhaps place a breakpoint wherever you're calling [UIImage imageNamed:] and see whether you're passing a nil or an empty string as an argument.

FYI, to create a breakpoint, simply click on the left edge of the code editor and add the blue breakpoint. Then right click on the breakpoint and select "Edit Breakpoint.." from the menu that pops up. Then in the "Condition" box type something like this :

(!imageFileName || [imageFileName isEqualToString:@""])

where imageFileName is the string representing the image file that you are trying to load.

like image 1
Raz Avatar answered Oct 16 '22 15:10

Raz