ios - 应用程序卡住,直到下载并显示图像。解析

标签 ios objective-c asynchronous parse-platform

我正在使用Parse.com
当我在viewDidAppear中下载PFFile(包含图像)时,我的应用程序冻结了一段时间,然后显示了图片。但这是不对的。我希望UIActivityIndi​​catorView动画或至少显示。我知道这涉及异步或其他问题。但是我不知道如何使它正常工作。

PFFile* imageFile = [[PFUser currentUser] objectForKey:@"profilePic"];
if (imageFile) {
self.activity.hidden = NO;
[self.activity startAnimating];
NSURL* imageFileUrl = [[NSURL alloc]initWithString:imageFile.url];
NSData* imageData = [NSData dataWithContentsOfURL:imageFileUrl];
self.profilePic.image = [UIImage imageWithData:imageData];
}

这将下载图像并显示它。

UPD:
PFQuery* query = [PFUser query];
[query valueForKey:@"profilePic"];
[query findObjectsInBackgroundWithBlock:^(NSArray* data, NSError*     error)
{
PFFile* imageFile = data[0];
[imageFile getDataInBackgroundWithBlock:^(NSData* data,NSError* error){
if (!error) {
self.activity.hidden = NO;
[self.activity startAnimating];
 NSURL* imageFileUrl = [[NSURL alloc]initWithString:imageFile.url];
 NSData* imageData = [NSData dataWithContentsOfURL:imageFileUrl];
 self.profilePic.image = [UIImage imageWithData:imageData];
 }else{
 self.profilePic.image = [UIImage imageNamed:@"profile@2.png"];
 }

 }];

 }];

UPD 2:

这解决了我的问题。这两个答案都是有帮助的。
PFQuery* query = [PFUser query];
[query getObjectInBackgroundWithId:[PFUser currentUser].objectId block:^(PFObject* object, NSError* error){
if(!error){
PFFile* imageFile = object[@"profilePic"];
[imageFile getDataInBackgroundWithBlock:^(NSData* data, NSError* error)      {
if (!error) {
self.activity.hidden = NO;
[self.activity startAnimating];
self.profilePic.image = [UIImage imageWithData:data];
NSLog(@"Profile pic shown");
}
else{
NSLog(@"Error 2: %@",error);
}
}];

}else{
self.profilePic.image = [UIImage imageNamed:@"profile@2.png"];
NSLog(@"Fail 1 : %@",error);
}
}];

最佳答案

这是因为您没有在后台获取对象。尝试使用查询来完成此操作。如果您需要更多信息,我可以在我的应用程序中包含以下代码来提供完整的查询:)

样例代码:

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

要获取带有数据的图像:
PFFile *imageFile = [object objectForKey:@"YOURKEY"];
            [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {}];

关于ios - 应用程序卡住,直到下载并显示图像。解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32039139/

相关文章:

javascript : calculating and returning values after Image loaded

javascript - "callback is not a function"Node.js

ios - GCD 全局并发队列并不总是并发(iOS 设备)?

ios - 如何保持启动画面直到应用程序委托(delegate)中的进程结束?

objective-c - 在线 xcode 项目(一起工作)

ios - Objective C - 每个 View Controller 的全局变量

javascript - async/await 不适用于 promisify

ios - 在 UITableViewCell 内动画 UILabel 文本更改

ios - 对于 iOS,是否可以运行 git 命令?

objective-c - 如何在 cocoa (osx)中检测顺时针/逆时针鼠标/光标拖动?