私が持っているのは、ファイル名の配列です。コレクションビューセルを埋めるために使用したコードは次のとおりです。
-(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 です。セルの 1 つをクリックすると、実行時エラーが発生します。私は何を間違っていますか?
編集
これは私が今持っているコードです。
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;
}