2013-03-20 1 views
1

외부 API에서 이미지를 가져 와서 UICollectionView & UIImageView 셀을 뷰에 바인딩하려고합니다. 데이터를 가져 와서 로그 파일에 인쇄 할 수 있습니다. 그러나 UICollectionView에서 이미지를 볼 수 없습니다. 여기 내 데이터 바인딩 코드입니다.나머지 API 데이터 문제로 UICollectionView 바인딩하기

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ImagesViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 

    // imagesArray is an array with serialized json data. 

    NSDictionary *finalImages = [self.imagesArray objectAtIndex:indexPath.row]; 

    NSLog(@"Entering collection view....."); 

    [[cell imageViewCell]setImage:[UIImage imageNamed:[finalImages valueForKey:@"link"]]]; 

    return cell; 
} 

데이터가 JSON 형식으로 제공됩니다.

data 
{ 
    abc: 'abc', 
    xyz: 'xyx', 
    link: 'link to an online image' 
} 

답변

0

imageNamed는 번들 (image.png)의 이미지 파일에서 이미지를 가져 오는 방법입니다. 당신이 URL에서 이미지를 얻을 씨 Richhard 브라운 응답으로 다운로드하거나 SDWebImage (이미지 뷰와 직접 설정)를 사용하려는 경우

샘플 colectionView SDWebImage 코드 (nonARC) :

+0

- (UICollectionViewCell *) collectionView : (UICollectionView *) cv cellForItemAtIndexPath : (NSIndexPath *) indexPath가 어떤 이유로 실행되지 않습니다. 이 이벤트에 로그를 기록하려고 시도했지만 내 콘솔에 인쇄되지 않습니다. – Harish

+0

확실히 collectionView를위한 setDatasource를 사용하거나 데이터 소스를 viewController로 드래그 하시겠습니까? –

+0

늦게 답장을 드려 죄송합니다.하지만 제공된 aaa 및 예제는 무엇이 잘못되었는지 파악하는 데 매우 유용합니다. 고마워. – Harish

0

UIImage imageNamed:은 URL이 아닌 파일 이름을 사용합니다.

이미지 파일을 표시하기 전에 수동으로 이미지 파일을 NSData 개체로로드해야합니다.

NSData dataWithContentsOfURL이 작업을 수행합니다.

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[finalImages valueForKey:@"link"]]]; 
+0

https://github.com/lequysang/github_zip/blob/master/CollectionViewNonARC.zip 너무 감사 응답을 위해 많이. 그렇게 많은 의미가 있습니다. 그러나 로그 메시지를 인쇄하고 로그 파일의 데이터를 보려고합니다. 그것은 콘솔에 아무것도 인쇄하지 않습니다. 처음에는 사건을 일으키지 않는 것 같습니다. 나는 .net 배경에서 왔기 때문에 내 용어를 용서해. – Harish