shreyashirday
shreyashirday

Reputation: 900

Parse loading Image into UIImageView iOS

Here is the code for the data I am passing on to the scene with the image:

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

And here is the code which loads the image:

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

For some reason, the image is not loading, why is this is so? How can I fix it?

Upvotes: 0

Views: 235

Answers (1)

Logan
Logan

Reputation: 53112

This:

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

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

Needs to be:

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

Because:

// 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];

However, this seems like there's an overall problem with your design pattern. If you already have access to objects, you shouldn't have to requery the entire database each time. Do you have a reference to the array populating the tableView? If so, something like this:

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

}

Upvotes: 2

Related Questions