Search code examples
iosobjective-cuicollectionviewflickr

show image from url which is in a nsstring in new xib


i'm working on a flickr thingy so when i press a image it will open the image in a new window. so far i've gotten it to pass the url to the next controller in line but the viewing is making it crash. i have a xib "window" which holds the image view i also tried using storyboard but somehow i couldn't get it to attach the image view once i placed it... here's some code

FlickrGalleryViewController.m

- (void)loadFlickrPhotos
{
photoURLs           = [[NSMutableArray alloc] init];
photoSmallImageData = [[NSMutableArray alloc] init];
photoURLsLargeImage = [[NSMutableArray alloc] init];

// 1. Build your Flickr API request w/Flickr API key in FlickrAPIKey.h
NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=%@&photoset_id=%@&format=json&nojsoncallback=1", FlickrAPIKey2, photoid];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"url = %@", url);
// 2. Get URLResponse string & parse JSON to Foundation objects.
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
// 3. Pick thru results and build our arrays
NSArray *photos = [[results objectForKey:@"photoset"] objectForKey:@"photo"];
for (NSDictionary *photo in photos) {
    // 3.b Construct URL for e/ photo.
    NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_s.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];


    [photoURLs addObject:[NSURL URLWithString:photoURLString]];
    NSLog(@"%lu", (unsigned long)[photoURLs count]);
    NSLog(@"photoURLString: %@", photoURLString);

    [photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]];
    // Build and save the URL to the large image so we can zoom
    // in on the image if requested
    photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg",
     [photo objectForKey:@"farm"], [photo objectForKey:@"server"],
     [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];

    [photoURLsLargeImage addObject:[NSURL URLWithString:photoURLString]];

    NSLog(@"photoURLsLareImage: %@\n\n", photoURLString);
}
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 NSLog(@"didselect");
  flickerImageViewController *viewControllerB = [[flickerImageViewController alloc] initWithNibName:@"flickerImageViewController" bundle:nil];
  viewControllerB.photoLargeInfo = [photoURLsLargeImage objectAtIndex:indexPath.row];
  [self.navigationController pushViewController:viewControllerB animated:YES];
}

flickerImageViewController.m here's my problem. when i set a breakpoint at flickrImage. it's show flickrImage is nil and photoLargeInfo has a url.

- (void)viewDidLoad
{
    [super viewDidLoad];

    flickrImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoLargeInfo]]];



}

(lldb) po flickrImage.image nil (lldb)

(lldb) po photoLargeInfo http://farm4.static.flickr.com/3803/10798724923_e5c539a520_m.jpg (lldb)

here's the error it's throwing at me

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance 0xb2dbf70'


Solution

  • Your photoURLsLargeImage array contains NSURL not NSString

    So better make photoLargeInfo to NSURL type and use it

    in .h

    @property(nonatomic, strong) NSURL *photoLargeInfo;
    

    in .m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        flickrImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoLargeInfo]];
    }