문제

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.

도움이 되었습니까?

해결책 2

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];

}

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top