UIImageView and NSMutableArray help

Discussion in 'iOS Programming' started by ctyhntr, Feb 23, 2011.

  1. ctyhntr macrumors 6502

    ctyhntr

    Joined:
    Jul 21, 2010
    #1
    I have two IBOutlets pulling from the same NSMutable Array. The NSMutable is storing the name of png files. file1.png, file2.png, etc

    Basically, its a pickerview and showing a picture and the file name (UIImageView and UILabel).

    The UILabel IBOutlet for the filename works, displaying the filename, but not UIImageView IBOutlet but crashes with a SIGABRT.

    [labelRace setText:[arrRace objectAtIndex:row]];
    The UILabel returns the filename, file1.png, file2png, etc

    [imageRace setImage:[arrRace objectAtIndex:row]];
    This doesn't work, instead the app crashes I get a SIGABRT

    and neither does this
    [imageRace setImage:[UIImage imageNamed:mad: "%@",arrRace objectAtIndex:row]];

    Hardcoding works, and confirms that the file is registered as a resource.
    [imageRace setImage: [UIImage imageName:mad:"file1.png"]];

    Any ideas what I'm doing wrong?
     
  2. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    That should work. You just need a stringWithFormat to get the name into it

    [imageRace setImage:[UIImage imageNamed:[NSSttring stringWithFormat: "%@",[arrRace objectAtIndex:row]]]];
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    That's redundantly redundant. It also has syntax problems.

    If the array contains NSString objects, then this should be the NSString directly from the array:
    Code:
    [arrRace objectAtIndex:row]
    
    and this will be the image:
    Code:
    [UIImage imageNamed:[arrRace objectAtIndex:row]]
    
    There's no need to go through the extra level of a formatted string. Worst-case:
    Code:
    [[arrRace objectAtIndex:row] description]
    
    gets you the same NSString * as:
    Code:
    [NSString stringWithFormat: @"%@",[arrRace objectAtIndex:row]]
    
    because %@ sends any object a description message. See the reference doc for String Format Specifiers.

    Or you could use the fact that you just assigned the correct name to a UILabel's text property:
    Code:
    [imageRace setImage:[UIImage imageNamed:labelRace.text]];
    
     
  4. RonC, Feb 24, 2011
    Last edited: Feb 24, 2011

    RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #4
    2 thoughts on 2 different problems:

    Problem 1: This code
    Code:
    [imageRace setImage:[arrRace objectAtIndex:row]];
    is wrong.

    imageRace.image is a UIImage, not a NSString. Change it to look like
    Code:
    imageRace setImage: [UIImage imageNamed:[arrRace objectAtIndex:row]];
    Problem 2: The string doesn't what you think it contains.

    The text ' file1.png', 'file1.png', and 'file1.png ' all look the same in the label, but they're not all names of files. Stick a NSLog statement near either of those IBOutlet messages to see the full context of the string, something like
    Code:
    NSLog(@"The string at row %d is '%@'.",row,[arrRace objectAtIndex:row]);
    Ron C
     
  5. ctyhntr thread starter macrumors 6502

    ctyhntr

    Joined:
    Jul 21, 2010
    #5
    RonC, chown33, and ulbador,

    Thank you for your help. I was able to fix it. Later, I'll get a cup of coffee and sit down to compare, so I can figure out why I didn't get it.
     

Share This Page