1

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.

Dovahkiin
  • 1,239
  • 11
  • 23
  • what does the picture look like from the backend? is it a case of compression quality loss or does it display properly in the backend? If so, it's probably just a case of threading, `loadInBackground` is called asynchronously and UI components need to happen on the main thread – soulshined Aug 30 '15 at 20:30
  • @soulshined the image on backend is perfectly fine. loadInBackground indeed, is called asynchronously, but let's say the network is slow, the imageView will just be a white blank view instead of black, until it is loaded. – Dovahkiin Aug 30 '15 at 20:34
  • So you load it in the background and when it's successful that's when you set `someImageView.image` to the profile picture in the call back – soulshined Aug 30 '15 at 20:36
  • FYI, there was a typo in my first comment. Should have stated, if _not_ a quality loss issue, it's potential a threading issue. – soulshined Aug 30 '15 at 20:37
  • @soulshined I'll give it a go. Also, this doesn't happen every time, sometimes it just worked fine, and sometimes it didn't. – Dovahkiin Aug 30 '15 at 20:39
  • Are you using `PFTableViewCell`? – soulshined Aug 30 '15 at 21:55
  • @soulshined no, just UITableViewCell – Dovahkiin Aug 30 '15 at 22:08

1 Answers1

1

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");
    }
}]; 
soulshined
  • 9,612
  • 5
  • 44
  • 79
  • 1
    i used to use `getDataInBackgroundWithBlock`, but have a look at this article: [Loading Images Stored on Parse](http://blog.parse.com/learn/engineering/loading-remote-images-stored-on-parse/) – Dovahkiin Aug 30 '15 at 21:14
  • 1
    yes @GellertLee , either way `PFFile` is data (see [here](https://parse.com/docs/ios/api/Classes/PFFile.html)), if you update your question with the method how you get 'user' or where you're actually setting the `PFImageView` in, I can edit it to suit your needs. This is a "one-size-fits-all" answer – soulshined Aug 30 '15 at 21:16
  • see my edited question. I set the PFImageView in xib of my custom cell. – Dovahkiin Aug 30 '15 at 21:29