1

我正在使用 Parse 作为我的应用程序的后端,并且个人资料照片似乎没有正确显示,如图所示: 注意约翰苹果籽

john_appleseed 的照片上有一条黑色条纹。

这是我保存个人资料图像的代码:

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
                      {

                      }
                  }];
             }
         }
     }];

这是我检索图像的方式:(在 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];
}

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

有任何想法吗?非常感谢。

4

1 回答 1

1

您应该在主线程上更新用户界面。由于您在后台加载某些内容,因此您应该通知主线程它需要更新对象。loadInBackground正在异步下载文件。

这是一个示例,您可以根据需要进行更改,以说明在回调中更新 UI 组件有其好处;这是基于 Parses 自己的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");
    }
}]; 
于 2015-08-30T20:59:13.150 回答