2014-03-30 29 views
0

我正在查詢我的分析數據庫以從特定表中下載圖像。 一切工作,除了我認爲是一個小故障。圖像在TableViewCells中不顯示,直到向下滾動

只有當我滾動UITableView一點時纔會出現圖像。

是的我正在異步下載圖像。

乾杯

UPDATE

PFQuery *imageQuery = [PFQuery queryWithClassName:@"WallImageObject"]; 
    //[imageQuery orderByAscending:@"createdAt"]; 
    [imageQuery whereKey:@"geoPoint" nearGeoPoint:currentLocationInGeoPoint withinKilometers:3.0]; 
    NSLog(@"PFGeoPoint current location: %f and %f", currentLocationInGeoPoint.latitude, currentLocationInGeoPoint.longitude); 
    [imageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){ 
     if (error) 
     { 
      NSLog(@"Error retreiving images: %@", error.localizedDescription); 
     }else 
     { 
      for (PFObject *wallImageObject in objects) { 
       WallImage *wallImage = [[WallImage alloc] init]; 
       wallImage.fbID = wallImageObject[@"UserId"]; 
       wallImage.username = wallImageObject[@"userName"]; 
       wallImage.createdAt = wallImageObject.createdAt; 

       [[NSOperationQueue pffileOperationQueue]addOperationWithBlock:^{ 
        wallImage.image = [UIImage imageWithData:[(PFFile *)wallImageObject[@"image"] getData]]; 
        [[NSNotificationCenter defaultCenter]postNotificationName:N_ImageDownloaded object:nil]; 
       }]; 

       wallImage.imageID = wallImageObject.objectId; 
       wallImage.geoPoint = wallImageObject[@"geoPoint"]; 
       NSLog(@"Image latitude: %f and londitude %f", wallImage.geoPoint.latitude, wallImage.geoPoint.longitude); 
       [[DataStore instance].wallImages addObject:wallImage]; 
      } 
      NSLog(@"%lu images in this location", (unsigned long)[[DataStore instance].wallImages count]); 
      if ([delegate respondsToSelector:@selector(communicationDidGetWallImages:)]) { 
       [delegate communicationDidGetWallImages:YES]; 
      } 
     } 

這裏,我顯示圖像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Post"; 
    FeedPhotoCell *feedPhotoCell = (FeedPhotoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    WallImage *wallImage = [DataStore instance].wallImages[indexPath.row]; 

    [feedPhotoCell.photoPost setImage:wallImage.image]; 

    NSString *facebbokUserID = wallImage.fbID; 
    NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large",facebbokUserID]; 
    NSURL *facebookProfilePicURL = [NSURL URLWithString:urlString]; 

    [feedPhotoCell.profilePhoto setImageWithURL:facebookProfilePicURL]; 
    feedPhotoCell.profilePhoto.layer.cornerRadius = 10.0; 
    feedPhotoCell.profilePhoto.layer.borderColor = [UIColor orangeColor].CGColor; 
    feedPhotoCell.profilePhoto.layer.borderWidth = 1.0; 
    feedPhotoCell.profilePhoto.clipsToBounds = YES; 

    NSLog(@"image cell height: %f", feedPhotoCell.photoPost.image.size.height); 

    return feedPhotoCell; 
} 
+1

如果可能,這將有助於提供用於下載和顯示圖像的代碼。 – Brian

+1

這聽起來像你在後臺線程上設置圖像。您可能必須將'imageView.image = ...'分派到主線程,以在表視圖重繪之前使更改生效。 – Eagerod

+0

我按照你的說法修復了它。我開始下載cellForRowAtIndex中的圖像,當它發出響應時,我將圖像加載到主線程中。謝謝 – Camus

回答

0

如果圖像佔用大量的內存,它們將被移除釋放內存。

然後,當您的表視圖試圖繪製它們時,它必須再次下載它們,這需要時間。

解決方案是將圖像下載到手機上的磁盤,然後從磁盤上的文件加載圖像。

+0

它不像數據被重新加載。如果我根本不滾動表格,圖像就不會顯示出來。但是,如果我稍微滾動表格,圖像就會顯示出來。 – Camus

相關問題