UICollectionView从UiImagePickerController获取图像

时间:2016-12-27 10:23:11

标签: ios objective-c uicollectionview uiimagepickercontroller

我有一个集合视图。我想为每个单元格上传五张图片,并且如果JSON已经存在,则还要将图片提取到该单元格中,如果不在UIImagePicker上使用UIButton,则必须逐个上传五张图片,它可以是四个,三个或一个。

如果已经存在的图像替换了之前的图像。像这样的东西

this is how i get

现在我想替换并更改这些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 个答案:

答案 0 :(得分:0)

您可以按照以下步骤实现此目的:

1)当您收到网络服务响应时,请制作一系列图片。在这里,对于您的演示,我拍摄了两个图像数组:jsonImgArraycurrentImgArray

获得回复后,请将您的图片放在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数据类型。

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