Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS iOS Foundations Storyboards Segue

passing an image in a segue

I'm trying to pass an image url in a segue to my destination view controller, and am having a hard time.

Here is the code

In the sender view controller:

if ([[segue identifier] isEqualToString:@"ShowImageIdentifier"]) { ShowImageViewController svc = (ShowImageViewController )[segue destinationViewController]; NSIndexPath indexPath = [_searchResultsTableView indexPathForSelectedRow]; NSString key = searchResults.allKeys[indexPath.row]; NSDictionary* dictionary = ((NSArray*)searchResults[key])[indexPath.row];

svc.stringImageURL = dictionary[@"image_url"];

}

In the destination view controller, in view did load:

NSURL *url = [NSURL URLWithString:_stringImageURL]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; self.imageImageView.image = img;

I am so lost. I tried everything. I initially tried to pass the dictionary directly and then convert the url to an image, and that didn't work either.

Stone Preston
Stone Preston
42,016 Points

can you post the code you are using

1 Answer

In the sender view controller:

if ([[segue identifier] isEqualToString:@"ShowImageIdentifier"]) { ShowImageViewController svc = (ShowImageViewController *)[segue destinationViewController]; NSIndexPath indexPath = [_searchResultsTableView indexPathForSelectedRow]; NSString* key = searchResults.allKeys[indexPath.row]; NSDictionary* dictionary = ((NSArray*)searchResults[key])[indexPath.row];

    svc.stringImageURL = dictionary[@"image_url"];

}

In the destination view controller, in view did load:

NSURL *url = [NSURL URLWithString:_stringImageURL]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; self.imageImageView.image = img;

I am so lost. I tried everything. I initially tried to pass the dictionary directly and then convert the url to an image, and that didn't work either.