Ios 应用程序冻结,直到图像下载并显示。作语法分析
我正在使用Parse.com 当我在ViewDid中下载我的PFFile(包含图像)时,我的应用程序会冻结一段时间,然后显示图片。但这是不对的。我希望UIActivityIndicatorView设置动画或至少显示动画。我知道这是关于异步的。但我不知道如何让它正常工作Ios 应用程序冻结,直到图像下载并显示。作语法分析,ios,objective-c,asynchronous,parse-platform,Ios,Objective C,Asynchronous,Parse Platform,我正在使用Parse.com 当我在ViewDid中下载我的PFFile(包含图像)时,我的应用程序会冻结一段时间,然后显示图片。但这是不对的。我希望UIActivityIndicatorView设置动画或至少显示动画。我知道这是关于异步的。但我不知道如何让它正常工作 PFFile* imageFile = [[PFUser currentUser] objectForKey:@"profilePic"]; if (imageFile) { self.activity.hidden = NO;
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:
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) {}];
您的代码有几处错误
[query valueForKey:@“profilePic”]
如果你想让用户拥有个人资料图片,那么你应该像这样做[querywherekeyexist:@“profilePic”]代码>,但此时您的查询将关闭所有对象
PFUser *currentUser = [PFuser currentUser];
if (!currentUser)
{
//need to login
}
else
{
// may be you need to fetch
__weak typeof(self) weakSelf = self;
[currentUser fetchIfNeededInBackgroundWithBlock:^(PFObject *obj, NSError *error){
PFFile* imageFile = [[PFUser currentUser] objectForKey:@"profilePic"];
[imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error)
{
weakSelf.profilePic.image = [UIImage imageWithData:imageData];
}
}];
}];
}
数据[0]
之前,必须检查数据.计数,如果数据数组为nil
或为空,则应用程序可能会崩溃
PFUser *currentUser = [PFuser currentUser];
if (!currentUser)
{
//need to login
}
else
{
// may be you need to fetch
__weak typeof(self) weakSelf = self;
[currentUser fetchIfNeededInBackgroundWithBlock:^(PFObject *obj, NSError *error){
PFFile* imageFile = [[PFUser currentUser] objectForKey:@"profilePic"];
[imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error)
{
weakSelf.profilePic.image = [UIImage imageWithData:imageData];
}
}];
}];
}
永远不要在主线程上访问Internet。如何在其他线程上访问?这就是问题pffile*imageFile=[[PFUser currentUser]objectForKey:@“profilePic”];[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 data WITHCONTENTSOFURL:imageFileUrl];self.profilePic.image=[UIImage imageWithData:imageData];}其他{self.profilePic.image=[UIImage imageNamed:@”profile@2.png"]; } }]; 不工作您需要将getDataInBackground放入查询中。开始查询,然后获取数据。检查问题。更新尝试将代码放入viewDidLoad。它不应该出现在视图中。嗯,你的问题写错了。。。。。试着去Parse.com了解更多关于它的信息。不知道为什么每个用户都有自己的profilePic(在我的例子中),我只想选择currentUser并获取它的数据。怎么做?嗯,不知道你写了什么uuu弱型(自我)。。。尝试失败,因为我的internet连接增长,并且我立即加载了所有文件。我要用它做点什么,然后试着用它的typeof u我不使用编辑器,你必须检查syntaxIt是typeof,但它也是不必要的。删除该行并参考方框中的
self
。弱自引用是为了避免保留循环,但自引用和块之间没有所有权链。我做了一些魔术,但我仍然不知道如何做,但它加载文件的速度要快得多。我希望这不是因为互联网连接。无论如何,我要用糟糕的网络测试并写下结果
PFUser *currentUser = [PFuser currentUser];
if (!currentUser)
{
//need to login
}
else
{
// may be you need to fetch
__weak typeof(self) weakSelf = self;
[currentUser fetchIfNeededInBackgroundWithBlock:^(PFObject *obj, NSError *error){
PFFile* imageFile = [[PFUser currentUser] objectForKey:@"profilePic"];
[imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error)
{
weakSelf.profilePic.image = [UIImage imageWithData:imageData];
}
}];
}];
}