2016-07-21 5 views
-1

UICollectionViewにflickr public imagesを表示する必要があります。私はすべてのことをしましたが、画像はUICollectionViewに読み込まれません。私は客観的に新しいです - c。助けてください。UICollectionViewに画像をロードできません

authorIds = [[NSMutableArray alloc] init]; 
    dateTaken = [[NSMutableArray alloc] init]; 
    smallImageUrlArray = [[NSMutableArray alloc] init]; 
    largeImageUrlArray = [[NSMutableArray alloc] init]; 
    smallImageArray = [[NSMutableArray alloc] init]; 

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
    layout.minimumLineSpacing = 10; 
    layout.minimumInteritemSpacing = 10; 
    layout.sectionInset = UIEdgeInsetsMake(110, 20, 20, 20); 
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; 
    [_collectionView setDataSource:self]; 
    [_collectionView setDelegate:self]; 
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 
    [_collectionView setBackgroundColor:[UIColor blackColor]]; 
    [self.view addSubview:_collectionView]; 



    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
    { 
     return totalImageNo; 
    } 


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

    UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100]; 

    cellImageView.image = [UIImage imageNamed:[smallImageUrlArray objectAtIndex:indexPath.row]]; 
    //[self.view addSubview:cellImageView]; 
    [cell.backgroundView addSubview:cellImageView]; 





    // NSLog(@"%@",[smallImageUrlArray objectAtIndex:indexPath.row]); //Here can show Img's values correctly 

    cell.backgroundColor=[UIColor darkGrayColor]; 
    return cell; 
} 

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return CGSizeMake(160, 160); 
    } 

以下では、Flickrイメージを取得して配列に格納しています。 UICollectionビューにsmallImageArrayをロードします。何も表示されません。

-(void)fetchFlickrPublicImages 
    { 
     NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=data"]; 

     NSURL *url = [NSURL URLWithString:urlString]; 

     NSData *badJSON = [NSData dataWithContentsOfURL:url]; 

     NSString *dataAsString = [NSString stringWithUTF8String:[badJSON bytes]]; 

     NSString *correctedJSONString = [NSString stringWithString:[dataAsString substringWithRange:NSMakeRange (15, dataAsString.length-15-1)]]; 
     correctedJSONString = [correctedJSONString stringByReplacingOccurrencesOfString:@"\\'" withString:@"'"]; 

     NSData *correctedData = [correctedJSONString dataUsingEncoding:NSUTF8StringEncoding]; 

     NSError* error; 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:correctedData options:kNilOptions error:&error]; 

     NSArray *images = [json objectForKey:@"items"]; 

     for (NSDictionary *image in images) 
     { 
      NSString *authorId = [image objectForKey:@"author_id"]; 

      [authorIds addObject:(authorId.length > 0 ? authorId: @"Untitled")]; 

      NSString *largeImageUrl = [NSString stringWithFormat:@"%@",[[image objectForKey:@"media"] objectForKey:@"m"]]; 

      NSString *smallImageUrl = [largeImageUrl stringByReplacingOccurrencesOfString:@"_m" withString:@"_s"]; 

      [smallImageUrlArray addObject:smallImageUrl]; 

      [dateTaken addObject:[NSString stringWithFormat:@"%@",[image objectForKey:@"date_taken"]]]; 

      [largeImageUrlArray addObject:[NSString stringWithFormat:@"%@",[[image objectForKey:@"media"] objectForKey:@"m"]]]; 

      totalImageNo = authorIds.count; 
     } 


     for (int i=0; i< smallImageUrlArray.count; i++) { 

      UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL: 
                 [NSURL URLWithString:[smallImageUrlArray objectAtIndex:i]]]]; 

      [smallImageArray addObject:image]; 
     } 
     NSLog(@"%@", smallImageArray); 

     NSLog(@"%ld", (long)totalImageNo); 
     // NSLog(@"authorIds: %@\n\n", authorIds); 
     // NSLog(@"photoURLsLareImage: %@\n\n", smallImageUrlArray); 
     // NSLog(@"photoURLsLareImage: %@\n\n", largeImageUrlArray); 

    } 
+0

変更この '[cellImageView cell.backgroundView addSubview]のようなコードを記述します。 contentView addSubview:cellImageView]; 'と試してみてください –

+0

何も表示されません:( –

答えて

-1

あなたはコレクションビューセルのcontentView、NOT backgroundViewにあなたのUIImageViewを追加する必要があります。あなたのコードで

-1

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100]; 
cellImageView.image = [UIImage imageNamed:[smallImageUrlArray objectAtIndex:indexPath.row]]; 
[cell.backgroundView addSubview:cellImageView]; 
} 

あなたはURLとして画像を割り当てています。イメージを直接ダウンロードしているfetchFlickrPublicImagesという名前のメソッド内にあります。 `に` [セル;:

は、より良いあなたは

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100]; 
cellImageView.image =(UIImage*)[smallImageArray objectAtIndex:indexPath.row]; 
[cell.contentView addSubView cellImageView]; 
} 
関連する問題