【问题标题】:how to add the camera capture images in array in ios如何在ios中将相机捕获的图像添加到数组中
【发布时间】:2025-12-22 17:15:12
【问题描述】:

想要添加添加到数组中的相机捕获图像并将该数组加载到集合视图中显示所有图像

///here i get the capture image in picker

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
      UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
      imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
       imagePicker.delegate = self;
       imagePicker.allowsEditing = YES;
       [self presentViewController:imagePicker animated:YES completion:nil];
 }


- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

     // in this part how can add the camera capture image in array  
     // and load that array value in collection view..
}

帮助我如何实现这一目标...

【问题讨论】:

    标签: ios iphone uiimagepickercontroller


    【解决方案1】:

    我还没有测试我要写的东西,但你可以试试这个:

    -(void)viewDidLoad {
    
       //declare before NSMutableArray *_mutableArray;
       _mutableArray = [[NSMutableArray alloc] init];
    }
    
    ...
    
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
       //dismiss UIImagePickerController
       [picker dismissViewControllerAnimated:YES completion:Nil];
    
       //take image from info
       UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
       //store image into NSMutableArray
       [_mutableArray addObject:image];
    
       //reload collectionView
       [self.collectionView reloadData];
    }
    

    collectionView cellForItemAtIndexPath

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
       //this is a custom CollectionViewCell
       CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
       cell.imageA.image = [_mutableArray objectAtIndex:indexPath.row];
    
       return cell;
    
    }
    

    【讨论】:

    • 完全正确的代码...我在 cell.imageA.image = [_mutableArray objectAtIndex:indexPath.row] 中犯了错误;部分感谢你...
    • @srini imageA 是我在自定义单元格中的自定义图像.. 无论如何很高兴能提供帮助! :)
    最近更新 更多