将图像加载到UIImageView iOS中

时间:2014-04-08 23:24:59

标签: ios objective-c uiimageview parse-platform

以下是我传递给场景的数据代码:

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

由于某种原因,图像没有加载,为什么会这样?我该如何解决?

1 个答案:

答案 0 :(得分: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];

}
相关问题