UICollectionView への画像の読み込みに問題があります。写真を撮り、特定の名前でドキュメント フォルダーに保存しようとしています。写真の撮影が終了した後、この写真をコレクション ビューに表示したいと考えています。
これまで、画像はドキュメント フォルダーに保存され、_stepImagesArray に自動的に追加されていましたが、それらの画像を読み込んで uicollectionview に取り込む方法がわかりません。
助けてください、私の試行錯誤にはしばらく時間がかかりました:)
StepsCollectionViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
_stepImagesArray = [[NSMutableArray alloc] init];
}
#pragma mark - UICollectionView settings
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection: (NSInteger)section {
return _stepImagesArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.imageCell.image = [UIImage imageNamed:[_stepImagesArray objectAtIndex:indexPath.row]];
cell.labelCell.text = _stepImagesArray[indexPath.row];
return cell;
}
- (IBAction)takePhoto
{
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:photoPicker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)photoPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *selectedImage = [info valueForKey:UIImagePickerControllerOriginalImage];
[self.selectedImageView setImage:selectedImage];
arrayCount = self.stepImagesArray.count;
NSLog(@"array count is: %i",arrayCount);
NSString *jpgImageName = [NSString stringWithFormat:@"Documents/FolderName_%@%i.jpg", passedStepName, arrayCount+1];
NSString *jpgImageNameForArray = [NSString stringWithFormat:@"FolderName_%@%i.jpg", passedStepName, arrayCount+1];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent: jpgImageName];
[UIImageJPEGRepresentation(selectedImage, 0.1) writeToFile:jpgPath atomically:YES];
// Let's check to see if files were successfully written...
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
[_stepImagesArray addObject:jpgImageNameForArray];
[photoPicker dismissViewControllerAnimated:YES completion:nil];
}
CollectionViewCell.h
@interface CollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageCell;
@property (weak, nonatomic) IBOutlet UILabel *labelCell;
@end