0

I have a CollectionView setup that displays remote image data from server. I am able to view the images in the CollectionView, but nothing shows when I tap the item for the DetailView. It was working properly while I used local images, but after adding the "SDWebImage framework", I am unable to view the images when I tap them for DetailView. I believe the issue lies with the prepareForSegue method, but nothing I have tried will solve the issue. I have searched many questions on "StackOverflow", but was unable to find an answer for this specific issue.

Here is my code for CollectionViewController:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    [cell.thumbnailView setImageWithURL:[NSURL URLWithString:[photos objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"logo.png"] options:SDWebImageRefreshCached];

    return cell;

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"displayDetail"]) {
        PhotoViewController *destViewController = (PhotoViewController *)segue.destinationViewController;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
        NSString *imagePath = [photos objectAtIndex:indexPath.row];
        UIImage *image = [UIImage imageNamed:imagePath];
        NSLog(@"Image named: %@ at row %ld", imagePath, (long)indexPath.row);
        destViewController.largeImage = image;
    }
}

The segue triggers fine, but just displays a white screen in PhotoViewController.m after being tapped.

Tulon
  • 4,011
  • 6
  • 36
  • 56
John Blanchard
  • 195
  • 2
  • 11
  • If you log image in prepareForSegue, does it give you the right thing? How are you displaying the image in the PhotoViewController -- you're passing an image, do you set that on an image view? – rdelmar Apr 29 '14 at 20:27
  • @rdelmar The NSLog I have in prepareForSegue shows `Image named: http://192.168.22.31:9999/IMG_0001.JPG at row 1`, which is the correct URL for the image. – John Blanchard Apr 29 '14 at 20:53
  • @rdelmar `largeImage` is declared as a `UIImage` property in `PhotoViewController.h`, and displayed in `viewDidLoad` as `imageView.image = self.largeImage;` – John Blanchard Apr 29 '14 at 20:54
  • I asked about logging image, not imagePath. If you log image, doe sit give you a UIImage object? – rdelmar Apr 29 '14 at 21:04
  • @rdelmar Sorry, I misunderstood. If I log `NSLog(@"%@", image);` it shows `(null)`. – John Blanchard Apr 29 '14 at 21:11

2 Answers2

1

From what I see in your code, you build up your thumbnailView using the setImageWithURL method of the SDWebImage class.

Then when you build up your large view, you pass the url of the image to [UIImage imageNamed:imagePath].

From the documentation of SDWebImage, I see it's an extension to UIImageView. So you can simply call [destViewController.imageView setImageWithURL:...] in your segue preparation.

Your prepareForSegueshould then look something like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"displayDetail"]) {
        PhotoViewController *destViewController = (PhotoViewController *)segue.destinationViewController;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
        NSString *imagePath = [photos objectAtIndex:indexPath.row];
        NSLog(@"Image named: %@ at row %ld", imagePath, (long)indexPath.row);

        [destViewController.imageView
                     setImageWithURL:[NSURL URLWithString:imagePath]
                    placeholderImage:[UIImage imageNamed:@"logo.png"] 
                             options:SDWebImageRefreshCached];
    }
}

Edit:

In the viewDidLoad of your PhotoViewController you will not have to do anything about this. Just make sure you import #import <SDWebImage/UIImageView+WebCache.h> in the file where you implemented prepareForSegue.

riisemichi
  • 123
  • 6
  • Using this method, do I just add `#import ` and pass `[self imageView];` in `PhotoViewController.m`, or would I need to create another `CollectionView` to display the image? Sorry, I'm new to iOS and this is the first time I've used a `CollectionView`. – John Blanchard Apr 29 '14 at 21:30
  • @JohnBlanchard Please take a look at my recent edit. Let me know if it solved your problem. – riisemichi Apr 30 '14 at 09:48
0

I ended up reworking quite a bit of how I was displaying the data, and managed to get it to work.

Here's the code I used:

CollectionViewController.h

#import <UIKit/UIKit.h>
#import "PhotoViewController.h"
#import "CollectionViewCell.h"

@class PhotoViewController;

@interface CollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) PhotoViewController *photoViewController;

@end

CollectionViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"displayDetail"]) {
    PhotoViewController *destViewController = segue.destinationViewController;
    NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];
    NSString *largeImageURL = [photos objectAtIndex:indexPath.row];
    destViewController.imageURL = [NSURL URLWithString:largeImageURL];
    }
}

PhotoViewController.h

#import <UIKit/UIKit.h>
#import "CollectionViewController.h"

@interface PhotoViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) NSURL *imageURL;

@end

PhotoViewController.m

#import "PhotoViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController

@synthesize imageURL = _imageURL;
@synthesize imageView = _imageView;

#pragma mark - Managing the detail item

- (void)setImageURL:(NSURL *)imageURL
{
    if (_imageURL != imageURL)
    {
        _imageURL = imageURL;
        [self configureView];
    }
}

- (void)configureView
{
    if (self.imageURL)
        // NSLog(@"%@", self.imageURL);
    {
        // NSLog(@"%@", self.imageView);
        [self.imageView setImageWithURL:self.imageURL placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize)
         {
         }
             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
         {
             // NSLog(@"%@", image);
         }];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureView];

}
John Blanchard
  • 195
  • 2
  • 11