Tushar
Tushar

Reputation: 5945

Help needed in the following code

Can we assign the value of an object of an array having an image value to a variable of image view, see the following code

NSArray *imgArray=[[NSArray alloc] initWithObjects:@"Bingo2.png", nil];
    UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    img.image=[imgArray objectAtIndex:0];    //line 3
    [self.view addSubview:img];

its not working, Application is crashing i guess because of line 3

Please help me, Many Thanks for the help.

Upvotes: 1

Views: 75

Answers (3)

Robin
Robin

Reputation: 10011

use this

img.image= [UIImage imageNamed:[imgArray objectAtIndex:0]];

instead of

img.image=[imgArray objectAtIndex:0];

if the above doesn't work

than you can also use this

NSString *string = [NSString stringWithFormat:@"%@", [imgArray objectAtIndex:0]];
img.image= [UIImage imageNamed:string];

Upvotes: 3

David
David

Reputation: 14404

You are storing an NSString object in the array and not an image. That is why it is crashing.

Upvotes: 4

omz
omz

Reputation: 53561

You have a string of a filename in your array, not an image. Use [UIImage imageNamed:@"Bingo2.png"] instead.

Upvotes: 1

Related Questions