gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:30:16 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com