iOS解析配置文件图片上的黑色条带(PFImageView)
我正在使用Parse作为我的应用程序的后端,并且配置文件照片似乎没有正确显示,如图所示: 约翰·阿普利塞德的照片上有一条黑色条纹 以下是我保存配置文件图像的代码:iOS解析配置文件图片上的黑色条带(PFImageView),ios,objective-c,parse-platform,pfimageview,Ios,Objective C,Parse Platform,Pfimageview,我正在使用Parse作为我的应用程序的后端,并且配置文件照片似乎没有正确显示,如图所示: 约翰·阿普利塞德的照片上有一条黑色条纹 以下是我保存配置文件图像的代码: NSData *profileData = UIImagePNGRepresentation(cell1.profileView.image); PFFile *profileFile = [PFFile fileWithName:@"profilePhoto" data:profileData]; [profileFile sav
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内)
在-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject*)对象中
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];
有什么想法吗?非常感谢。您应该在主线程上更新用户界面。由于您在后台加载了一些东西,所以应该通知主线程它需要更新一个对象
loadInBackground
正在异步下载文件
下面是一个示例,您可以根据自己的需要进行更改,只是为了说明,在回调中更新UI组件有其好处;这是基于自己的分析:
从后端看,图片是什么样子的?这是压缩质量损失还是在后端正确显示?如果是这样的话,这可能只是线程的一种情况,
loadInBackground
是异步调用的,UI组件需要发生在主服务器上thread@soulshined后端的图像非常好。loadInBackground确实是异步调用的,但假设网络速度较慢,在加载之前,imageView将只是一个白色的空白视图,而不是黑色的。因此,您在后台加载它,成功后,在调用中将someImageView.image
设置为配置文件图片,我的第一条评论中有一个拼写错误。应该说明的是,如果不是质量损失问题,可能是线程问题。@soulshined我会试一试。而且,这并不是每次都会发生,有时它工作得很好,有时则不是。我曾经使用过getDataInBackgroundithBlock
,但看看这篇文章:yes@GellertLee,无论哪种方式PFFile
都是数据(请参阅),如果您使用如何获取“用户”或在中实际设置PFImageView
的方法更新问题,我可以根据您的需要对其进行编辑。这是一个“一刀切”的答案见我编辑的问题。我在自定义单元格的xib中设置PFImageView。
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];
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");
}
}];