我在将图像加载到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