无法将图像从集合视图控制器发送到详细信息视图控制器

时间:2013-03-31 14:37:44

标签: objective-c ios6 uicollectionview

我所拥有的是一组文件名。 这是我用来填充Collection View Cells的代码。

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

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReuseID" forIndexPath:indexPath];

NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/DrawnImage"];
NSArray *contentOfDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:NULL];


NSString *fileName = [contentOfDirectory objectAtIndex:indexPath.item];
NSString *path = [dataPath stringByAppendingPathComponent:fileName];


UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *cellImageView =  cell.collectionImageView;
cellImageView.image = image;   

return cell;

}

图像正在单元格中正确显示。我想要做什么,当我点击一个单元格时,该单元格中的图片必须全屏显示在详细视图控制器中。

我试过这个。

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
    NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];
            NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/DrawnImage"];
    NSArray *contentOfDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:NULL];
    NSString *fileName = [contentOfDirectory objectAtIndex:selectedIndexPath.item];
    NSString *pathToImage = [dataPath stringByAppendingPathComponent:fileName];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:image];
    DetailViewController *dvc = [segue destinationViewController];
    dvc.finalImage = sender;
    NSLog(@"%@", backgroundImageView);


}

}

finalImage是详细信息视图控制器中的UIImageView。单击其中一个单元格时出现运行时错误。我做错了什么?

修改

这是我现在的代码。

CollectionViewController.h

@property (nonatomic, retain) UIImage *sendImage;

CollectionViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
    NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];
            NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/DrawnImage"];
    NSArray *contentOfDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:NULL];
    NSString *fileName = [contentOfDirectory objectAtIndex:selectedIndexPath.item];
    NSString *pathToImage = [dataPath stringByAppendingPathComponent:fileName];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
    DetailViewController *dvc = [segue destinationViewController];
    dvc.passedInImage = image;
    image = dvc.passedInImage;
}
}

DetailViewController.h

@property (weak, nonatomic) IBOutlet UIImageView *finalImage;
@property UIImage *passedInImage;

DetailViewController.m

-(void)viewDidAppear:(BOOL)animated {

self.finalImage.image = passedInImage;
}

2 个答案:

答案 0 :(得分:2)

你做错了几件事。首先,prepareForSegue方法中的sender将是您单击的UICollectionViewCell,并且您尝试将其分配给UIImage视图。那是不可能做到的。但是,即使发件人是imageView,这也不是您想要做的。在prepareForSegue执行时,尚未加载详细控制器的视图,因此您无法访问其任何出口。你应该做的是在细节控制器中创建一个UIImage属性,例如调用它* passInImage。在prepareForSegue中,将图像分配给属性:

dvc.passedInImage = image;

然后,在dvc中,可能在viewDidAppear中,将finalImage的图像设置为passedInImage。

答案 1 :(得分:0)

dvc.finalImage = sender;

外,代码看起来很好

从var名称查看您正在设置图像属性,但发件人可能是集合视图单元格。您想改用backgroundImageView吗?