【问题标题】:UICollectionView getting Images from UiImagePickerControllerUICollectionView 从 UiImagePickerController 获取图像
【发布时间】:2016-12-27 10:23:11
【问题描述】:

我有一个收藏视图。我想为每个单元格上传五张图片,如果已经从JSON 中获取图片,如果在UIButton 上不使用UIImagePicker,则必须一张一张上传五张图片,可以是四张,三张或单个。

如果已经存在图像,则替换以前的图像。像这样的

现在我想替换和更改这些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");
}];

我怎样才能做到这一点?

【问题讨论】:

  • 您必须设置布尔值以检查新图像选择或现有图像更改
  • 如何使用附加到单元格的按钮更改图像..?
  • 稍等几分钟,我会给你完整的代码。
  • 谢谢哥们...等待
  • gist.github.com/himanshu-benzatine/… @Dheeraj 参考这个文件

标签: ios objective-c uicollectionview uiimagepickercontroller


【解决方案1】:

您可以按照以下步骤实现:

1) 当您收到 Web 服务响应时,请在其中制作一组图像。在这里,为了您的演示,我拍摄了两个图像数组:jsonImgArraycurrentImgArray

当您收到响应时,将您的图像放入 jsonImgArray 数组以及 currentImgArray 中。

2) 您可以将 currentImgArray 中的图像分配给您的 collectionViewCell 的 ImageView,就像您在上面所做的那样。这样,您将获得已上传的图像。

3) 如果你想改变图像,你可以写你collectionViewdidSelectItemAtIndexPath方法如下:

-(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 数据类型。

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 的数组图像。

【讨论】:

  • 什么是 selectedIndexPath ..?@Er.Vihar
  • @Dheeraj SelectedIndexPath 是一个 NSIndexPath 类型的对象,用于管理从 collectionView DidSelect 方法到图像视图的 didFinishPickingMediaWithInfo 方法的选定索引。
  • 你为什么要使用 JsonImgArray ,...?你没用它..?
  • 我没有在我的代码中提到它,但我在我的结论性声明中详细说明了如果你想跟踪你有没有改变任何图像而不是你需要的数组,因为该数组包含初始数据,即在任何更改之前,并且 currentImgArray 包含修改后的图像列表
  • cell.overlayButton.tag = indexPath.item;我必须在我的 cellForrowa 索引中处理这个吗...?
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2016-05-18
相关资源
最近更新 更多