如何在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”对象,您到底在哪里定义的