【问题标题】:UICollectionView PrepareForSegue Image not TransferringUICollectionView PrepareForSegue 图像未传输
【发布时间】:2014-02-24 19:47:30
【问题描述】:

我有一个 UICollectionView,我无法将图像传输到详细视图。当我使用本地图像和带有 [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

【问题讨论】:

    标签: ios objective-c json storyboard uicollectionview


    【解决方案1】:

    我在正在开发的应用程序中遇到了类似的问题,但找不到任何有关 Collection Views 的帮助。我使用的代码如下(已更新以匹配您的方法)。我希望这可以帮助您走上正轨。您还可以查看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];
    }
    

    【讨论】:

      【解决方案2】:

      只需访问所选单元格的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.
      }
      

      如果您需要更多帮助,请告诉我。

      【讨论】:

      • 此方法表示在对象类型 UICollectionViewCell 上找不到 cell.imageView.image 属性中的 imageView。
      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多