2016-07-21 8 views
-1

Ich brauche flickr öffentliche Bilder in einer UICollectionView anzuzeigen. Ich habe alle Sachen gemacht, aber Bilder laden nicht in UICollectionView. Ich bin neu im Ziel-c. Bitte helfen Sie.Fehler beim Laden des Bildes in einer 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); 
    } 

In unten werde ich Flickr Bilder abrufen und in Array speichern. möchte dann das smallImageArray in der UICollection-Ansicht laden. aber nichts zeigt sich.

-(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

Änderung dieses '[cell.backgroundView addSubview: cellImageView];' in '[Zelle. contentView addSubview: cellImageView]; 'und versuchen –

+0

nichts zeigt :( –

Antwort

-1

Sie sollten Ihre UIImageView die Sammlung Ansicht Zelle contentView, NICHT backgroundView hinzufügen.

-1

in Ihrem Code

- (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]; 
} 

Sie Bild als URL zuweisen. aber in Ihrer Methode namens fetchFlickrPublicImages, wo Sie Bilder direkt herunterladen.

besser schreiben Sie Code wie

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