Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ios中将图像从本地库添加到集合视图单元格中?_Ios_Uicollectionview_Uiimagepickercontroller_Uicollectionviewcell_Uiactionsheet - Fatal编程技术网

如何在ios中将图像从本地库添加到集合视图单元格中?

如何在ios中将图像从本地库添加到集合视图单元格中?,ios,uicollectionview,uiimagepickercontroller,uicollectionviewcell,uiactionsheet,Ios,Uicollectionview,Uiimagepickercontroller,Uicollectionviewcell,Uiactionsheet,我使用了UICollectionView来显示图像。单击“插入图像”按钮时,本地库中的照片应使用UIImagePicker添加到UICollectionCell。我在自定义单元格(即RACollectionViewCell中声明了UIImageView)。在didFinishPickingMediaWithInfo中,每当我以cell.imageview.image=chosenImage的身份写入时,UIImagePickerView的委托方法。我将错误作为未声明的标识符“cell”。请指导我

我使用了
UICollectionView
来显示图像。单击“插入图像”按钮时,本地库中的照片应使用
UIImagePicker
添加到
UICollectionCell
。我在自定义单元格(即
RACollectionViewCell
中声明了
UIImageView
)。在
didFinishPickingMediaWithInfo
中,每当我以
cell.imageview.image=chosenImage
的身份写入时,
UIImagePickerView
的委托方法。我将错误作为未声明的标识符“cell”。请指导我如何解决我的问题

任何帮助都是值得的,提前谢谢

这是我的密码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
            {
                static NSString *cellID = @"cellID";
                RACollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
                [cell.imageView removeFromSuperview];
                cell.imageView.frame = cell.bounds;
                cell.imageView.image = [UIImage imageNamed:[_photosArray objectAtIndex:indexPath.item]];
                cell.backgroundColor = [UIColor lightGrayColor];
                [cell.contentView addSubview:cell.imageView];

            UIButton *addBtn =[[UIButton alloc]init];
                 addBtn.tag=indexPath.row;
             deleteBtn.frame = CGRectMake(100, 100, 19, 19);
                           UIImage *img=[UIImage imageNamed:@"ic_add_photo.png"];
                           [addBtn setImage:img forState:UIControlStateNormal];
                           [addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
               [cell.contentView addSubview:addBtn];
            return cell;
            }
    -(void)addBtnAction
     {

        UIAlertController *actionsheetController = [[UIAlertController alloc]init];

            UIAlertAction *library = [UIAlertAction actionWithTitle:@"Choose from Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction
        * action)
                                      {
                                                // Library button tapped

                                                  [self performSelector:@selector(libraryBtnAction) withObject:nil];
                                           }];
                 [actionsheetController addAction:library];
            actionsheetController.view.tintColor = [UIColor darkGrayColor];

                 [self presentViewController:actionsheetController animated:YES completion:nil];
            }

            -(void)libraryBtnAction
            {
                 UIImagePickerController *picker  = [[UIImagePickerController alloc]init];
                 picker.delegate = self;
                 picker.allowsEditing = YES;
                 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                 [self presentViewController:picker animated:YES completion:nil];

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

                 chosenImage = info[UIImagePickerControllerEditedImage];
                 cell.imageView.image = chosenImage;
                 [_collectionView reloadData];

                 [picker dismissViewControllerAnimated:YES completion:NULL];

            }
  • 代码中的第一个问题是,您使用的是
    didFinishPickingMediaWithInfo
    中的“cell”对象,您到底在哪里定义的

  • 如何指定在采集单元的哪个图像视图中显示从库中拾取的图像

  • 只需使用标记跟踪内容,在拾取图像的最后,只需使用indexpath.item从特定标记加载单元格,然后重新加载该特定单元格