0

我拥有的是一组文件名。这是我用来填充集合视图单元格的代码。

    -(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 是 Detail View Controller 中的 UIImageView。单击其中一个单元格时出现运行时错误。我究竟做错了什么 ?

编辑

这是我现在拥有的代码。

集合视图控制器.h

@property (nonatomic, retain) UIImage *sendImage;

集合视图控制器.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;

细节视图控制器.m

-(void)viewDidAppear:(BOOL)animated {

self.finalImage.image = passedInImage;
}
4

2 回答 2

2

你做错了几件事。首先,prepareForSegue 方法中的 sender 将是您单击的 UICollectionViewCell,并且您正在尝试将其分配给 UIImage 视图。那是做不到的。然而,即使发送者是一个 imageView,这也不是你想要做的。在 prepareForSegue 执行的时候,细节控制器的视图还没有被加载,所以你不能访问它的任何出口。你应该做的是在细节控制器中创建一个 UIImage 属性,我们称之为 *passedInImage 。在 prepareForSegue 中,将图像分配给属性:

dvc.passedInImage = image;

然后,在dvc中,大概是在viewDidAppear中,设置finalImage的图片为passedInImage。

于 2013-03-31T15:35:43.017 回答
0

代码看起来不错,除了dvc.finalImage = sender;

这从您正在设置图像属性的 var 名称来看,但 sender 可能是一个集合视图单元格。你想backgroundImageView改用吗?

于 2013-03-31T14:55:28.530 回答