0

我在将图像加载到 UICollectionView 时遇到问题。我正在尝试拍摄一张照片并将其以特定名称保存到 Documents 文件夹中,并且我希望这张照片在照片拍摄关闭后出现在收藏视图中。

到目前为止,我的图像被保存在文档文件夹中并自动添加到 _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
4

0 回答 0