UICollectionView从UiImagePickerController获取图像
我有一个集合视图。我想为每个单元格上传五张图片,并且如果已经存在,则从JSON
获取图片到该单元格中,如果没有使用UIImagePicker
,则UIButton
动作必须一个一个地上传五张图片,它可以是四张,三张或单张图片。UICollectionView从UiImagePickerController获取图像
如果图像已经存在,则取代以前的图像。这样
东西现在我要替换和更改这些JSONImages
。 我已经在cellForRowAtIndexPath
MG_PhotoCollectionViewCell *cell = (MG_PhotoCollectionViewCell *) . [self.collectionView dequeueReusableCellWithReuseIdentifier:kProductCellIdentifier forIndexPath:indexPath];
NSString *myPatternString = [self.dataArray objectAtIndex:indexPath.item];
NSLog(@"myPatternString %@",myPatternString);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:myPatternString]];
cell.imageView.image = [UIImage imageWithData:data];
cell.backgroundColor = [UIColor lightGrayColor];
cell.overlayButton.layer.cornerRadius = cell.overlayButton.frame.size.height/2;
cell.overlayButton.clipsToBounds = YES;
[cell.overlayButton addTarget:self action:@selector(getImages:) forControlEvents:UIControlEventTouchUpInside];
cell.overlayButton.tag = indexPath.item;
做这样的事情,它选择器控制器
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info{
// UIImage *imagepick = info[UIImagePickerControllerEditedImage];
UIImage *image= info[UIImagePickerControllerEditedImage];
if (!image) image = info[UIImagePickerControllerOriginalImage];
[self.dataArray addObject:image];
[self.collectionView reloadData];
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Finished image picking");
}];
我怎样才能做到这一点?
您可以通过以下步骤实现:
1)当你的Web服务响应,使图像的数组你有它。在这里,为了演示,我拍摄了两个图像阵列:jsonImgArray
和currentImgArray
。
当您收到回复时,请将您的图片放入jsonImgArray
阵列以及currentImgArray
。
2)您可以像上面那样将图像从currentImgArray分配到您的collectionViewCell
的ImageView。由此,您将获得已上传的图像。
3)如果你想改变形象,你可以写你的collectionView
的didSelectItemAtIndexPath
方法如下:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[currentImgArray replaceObjectAtIndex:indexPath.row withObject:@""];
selectedIndexPath = indexPath;
// Code to manage change image as you have coded above
}
这里,selectedIndexPath是NSIndexPath
数据类型。
OR
3)你可以把你的按钮操作方法一样didSelectItemAtIndexPath
你只需要保持你的indexPath
。对于这一点,你可以指定collectionView
项目索引值对应的按钮的标签,这样你可以得到你indexPath
价值
-(void)btnAction:(UIButton *)sender
{
NSInteger item = sender.tag;
selectedIndexPath = [NSIndexPath indexPathForItem:item inSection:0];
}
现在
4)你可以改变你的形象didFinishPickingMediaWithInfo
方法的代码
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UICollectionView *collectionView;
UIImage *img = info[UIImagePickerControllerEditedImage];
[currentImgArray replaceObjectAtIndex:selectedIndexPath.row withObject:img];
[collectionView reloadItemsAtIndexPaths:@[selectedIndexPath]];
// You above code other that this functionality.
}
现在,如果您想检查图像中是否有任何更改,可以编写一些代码来比较jsonImgArray和currentImgArray的阵列图像。
你必须设置布尔值来检查新图像选择或现有图像更改 –
我该如何更改图像与附加到单元格的按钮..? – Nivesh
只需等几分钟,我会给你完整的代码。 –