UIImageView and NSMutableArray help

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

  1. ctyhntr macrumors 6502


    Jul 21, 2010
    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


    Feb 11, 2010
    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

    Aug 9, 2009
    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:
    [arrRace objectAtIndex:row]
    and this will be the image:
    [UIImage imageNamed:[arrRace objectAtIndex:row]]
    There's no need to go through the extra level of a formatted string. Worst-case:
    [[arrRace objectAtIndex:row] description]
    gets you the same NSString * as:
    [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:
    [imageRace setImage:[UIImage imageNamed:labelRace.text]];
  4. RonC, Feb 24, 2011
    Last edited: Feb 24, 2011

    RonC macrumors regular

    Oct 18, 2007
    2 thoughts on 2 different problems:

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

    imageRace.image is a UIImage, not a NSString. Change it to look like
    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
    NSLog(@"The string at row %d is '%@'.",row,[arrRace objectAtIndex:row]);
    Ron C
  5. ctyhntr thread starter macrumors 6502


    Jul 21, 2010
    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