iOS parse black strip on profile picture (PFImageView)

149 views Asked by At

I am using Parse as a backend of my app, and it seems that the profile photo is not displaying properly, shown in the image: notice the john appleseed

there is a black strip on john_appleseed's photo.

here is my code for saving the profile image:

NSData *profileData = UIImagePNGRepresentation(cell1.profileView.image);
PFFile *profileFile = [PFFile fileWithName:@"profilePhoto" data:profileData];
[profileFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
     {
          if (!error)
         {
             if (succeeded)
             {
                 [user setObject:profileFile forKey:@"profilePhoto"];
                 [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
                  {
                      if (!error)
                      {

                      }
                      else
                      {

                      }
                  }];
             }
         }
     }];

here is how I retrieve the image:(inside PFQueryTableViewController)

- (PFQuery *)queryForTable
{
    //NSLog(@"called");
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSString *filter = [defaults objectForKey:@"topicFilter"];
    NSLog(@"queryfortable: %@", filter);
    PFQuery *query = [PFQuery queryWithClassName:@"Questions"];
    [query includeKey:@"user"];
    [query whereKey:@"category" equalTo:filter];
    [query orderByDescending:@"createdAt"];
    return query;
}

- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == self.objects.count)
    {
        return nil;//this is for the load more cell.
    }
    return [self.objects objectAtIndex:indexPath.section];
}

in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object

PFUser *user = [object objectForKey:@"user"];
PFImageView *profileImgView = (PFImageView *)[cell viewWithTag:1];
profileImgView.layer.cornerRadius = profileImgView.frame.size.height/2;
profileImgView.layer.masksToBounds = YES;
PFFile *file = user[@"profilePhoto"];
profileImgView.file = file;
[profileImgView loadInBackground];

any ideas? many thanks.

1

There are 1 answers

3
soulshined On

You should be updating user interfaces on the main thread. Since your loading something in the background, you should notify the main thread that it needs to update an object. loadInBackground is downloading the file asynchronously.

Here is an example, that you can alter for your needs, just to illustrate, that updating UI components in the call back has it's benefits; this is based off of Parses own AnyPic:

NSString *requestURL = file.url; // Save copy of url locally (will not change in block)

[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if (!error) {
        //dispatch on main thread
        UIImage *image = [UIImage imageWithData:data];
    } else {
        NSLog(@"Error on fetching file");
    }
}];