UICollectionView PrepareForSegue图像不传输

时间:2014-02-24 19:47:31

标签: ios objective-c json storyboard uicollectionview

我有一个UICollectionView,我无法将图像传输到detailview。当我使用本地图像和带有[pictures [row]]方法的图片数组时,detailview传输将起作用,但由于使用SDWebImage框架,我不能这样做。一旦选择了单元格,我无法想象将图像正确地传输到细节视图。第一种方法CellForItemAtIndexPath解释了逻辑。选择的单元格和准备segue方法正在引发问题。如果您需要更多信息,请与我们联系。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    pictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    NSString *pictures1 = [[pictures objectAtIndex:indexPath.row]objectForKey:@"filename"];
    comment = [[pictures objectAtIndex:indexPath.row] objectForKey:@"comment"];

    [cell.imageView setImageWithURL:[NSURL URLWithString:pictures1]
                    placeholderImage:[UIImage imageNamed:@"Earth.png"]
    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
     {
         NSLog(@"Error downloaidng images");
     }];


   // UIImage *image;

   // int row = [indexPath row];

    //image = [UIImage imageNamed:pictures[row]];

    //image = [UIImage imageWithData:imageUrl];

   // cell.imageView.image = image;

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    UIImage *image;

    //int row = [indexPath row];

    NSString *pictures1 = [[pictures objectAtIndex:indexPath.row]objectForKey:@"filename"];
   // image = [UIImage imageNamed:pictures[row]];

    image = [UIImage imageNamed:pictures1];

    pictureDetailView *pictureDetail = [[pictureDetailView alloc]init];
    pictureDetail.image = image;

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UICollectionViewCell *)sender
{
    pictureDetailView *targetVC = (pictureDetailView *) [segue destinationViewController];
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];

    UIImage *image;

    //int row = [indexPath row];


    NSString *pictures1 = [[pictures objectAtIndex:indexPath.row]objectForKey:@"filename"];


   // image = [UIImage imageNamed:pictures[row]];

    image = [UIImage imageNamed:pictures1];

    //image = [UIImage imageWithData:imageUrl];

    targetVC.image=image;
}




@end

2 个答案:

答案 0 :(得分:2)

我在我正在处理的应用中遇到了类似问题,但找不到任何有关收藏视图的帮助。我使用的代码如下(更新以匹配您的方法)。我希望这有助于您走上正轨。您还可以查看SDWebImage GitHub page上发布的演示项目。这帮助我解决了其中的一些问题。

在您的CollectionViewController.h中:

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

@class PictureDetailView;

@interface CollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) PictureDetailView *detailViewController;

@end

在您的CollectionViewController.m中:

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

在您的DetailViewController.h

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

@interface DetailViewController : UIViewController 

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

@end

在您的DetailViewController.m

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

@interface DetailViewController ()

@end

@implementation DetailViewController

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


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

- (void)configureView
{
    if (self.imageURL)
    {
        [self.imageView setImageWithURL:self.imageURL
                       placeholderImage:nil
                        options:SDWebImageProgressiveDownload
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
         {
         }];
    }
}

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

答案 1 :(得分:1)

只需访问所选单元格的imageView并提取其图片即可。以下是如何做到这一点:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [self collectionView:collectionView cellForItemAtIndexPath:indexPath];
    UIImage *image = cell.imageView.image;

    // Pass the image to the next VC.
}

如果您需要更多帮助,请与我联系。