我有CollectionView
设置,显示来自服务器的远程图像数据。我可以在CollectionView
中查看图片,但在点按DetailView
的项目时没有显示任何内容。当我使用本地图像时它工作正常,但在添加" SDWebImage框架" 后,当我点击DetailView
时,我无法查看图像。我认为问题在于prepareForSegue
方法,但我尝试过的任何方法都无法解决问题。我在" StackOverflow"上搜索过很多问题,但无法找到这个特定问题的答案。
以下是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;
}
}
segue触发正常,但在点击后只在PhotoViewController.m
显示白屏。
答案 0 :(得分:1)
从我在代码中看到的内容,您使用setImageWithURL
类的SDWebImage
方法构建了thumbnailView。
然后,当您构建大型视图时,将图像的网址传递给[UIImage imageNamed:imagePath]
。
从SDWebImage的文档中,我看到它是UIImageView的扩展。因此,您只需在您的segue准备中致电[destViewController.imageView setImageWithURL:...]
。
您的prepareForSegue
应该是这样的:
- (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];
}
}
修改强>
在PhotoViewController的viewDidLoad
中,您不必对此做任何事情。
只需确保在实施#import <SDWebImage/UIImageView+WebCache.h>
的文件中导入prepareForSegue
。
答案 1 :(得分:0)
我最终重新修改了我显示数据的方式,并设法使其工作。
这是我使用的代码:
<强> 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];
}