2016-12-27 115 views
1

我有一個集合視圖。我想爲每個單元格上傳五張圖片,並且如果已經存在,則從JSON獲取圖片到該單元格中,如果沒有使用UIImagePicker,則UIButton動作必須一個一個地上傳五張圖片,它可以是四張,三張或單張圖片。UICollectionView從UiImagePickerController獲取圖像

如果圖像已經存在,則取代以前的圖像。這樣

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"); 
}]; 

我怎樣才能做到這一點?

+0

你必須設置布爾值來檢查新圖像選擇或現有圖像更改 –

+0

我該如何更改圖像與附加到單元格的按鈕..? – Nivesh

+0

只需等幾分鐘,我會給你完整的代碼。 –

回答

0

您可以通過以下步驟實現:

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數據類型。

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的陣列圖像。

+0

什麼是selectedIndexPath ..?@ Er.Vihar – Nivesh

+0

@Dheeraj SelectedIndexPath是一個NSIndexPath類型的對象,用於管理從collectionView的選擇的索引DidSelect方法來映射視圖的didFinishPickingMediaWithInfo方法。 –

+0

爲什麼你有使用JsonImgArray,...?你不使用它..? – Nivesh

相關問題