2014-04-08 65 views
0

這裏是我傳遞到現場與圖像數據的代碼:解析加載圖像到UIImageView的iOS版

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [super tableView:tableView didSelectRowAtIndexPath:indexPath]; 
    rowNo = indexPath.row; 
    PFUser *currentUser = [PFUser currentUser]; 
    PFQuery *query = [PFQuery queryWithClassName:@"Photo"]; 
    [query whereKey:@"username" equalTo:currentUser.username]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     _photo = [objects objectAtIndex:rowNo]; 
    }]; 

    [self performSegueWithIdentifier:@"showImageCloseup" sender:self]; 

} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    ImageCloseupViewController *destination = [segue destinationViewController]; 
    destination.photoObject = _photo; 
} 

,這裏是用來加載圖像的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    PFFile *p = [_photoObject objectForKey:@"photo"]; 
    [p getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { 
     if(!error){ 
      UIImage *image = [UIImage imageWithData:data]; 
      [_imageView setImage:image]; 
     } 
    }]; 

由於某種原因,圖像未加載,爲什麼是這樣?我該如何解決它?

回答

2

此:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    _photo = [objects objectAtIndex:rowNo]; 
}]; 

[self performSegueWithIdentifier:@"showImageCloseup" sender:self]; 

需要是:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    _photo = [objects objectAtIndex:rowNo]; 
    [self performSegueWithIdentifier:@"showImageCloseup" sender:self]; 
}]; 

因爲:

// Runs 1st - executes in background thread stores block, then runs block once it's done 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    // Runs 3rd - already segued at this point 
    _photo = [objects objectAtIndex:rowNo]; 
}]; 

// Runs 2nd - starts segue 
[self performSegueWithIdentifier:@"showImageCloseup" sender:self]; 

然而,這似乎像有你的設計模式的整體問題。如果您已經有權訪問對象,則不必每次都重新查詢整個數據庫。你有一個引用填充tableView的數組嗎?如果是這樣,這樣的事情:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [super tableView:tableView didSelectRowAtIndexPath:indexPath]; 
    rowNo = indexPath.row; 
    _photo = yourDataArray[indexPath.row]; 
    [self performSegueWithIdentifier:@"showImageCloseup" sender:self]; 

} 
+0

很酷,這個工程。我試圖接受你的答案,但它一直說「在一分鐘內接受」lol – shreyashirday

+0

沒問題@ user3140562 - 我在底部做了一個更新,你應該看看,我認爲這將對你有長遠的幫助。祝好運與您的其餘項目! – Logan