这是我的代码,我遇到了问题。它可以工作,但加载需要很长时间。我在viewDidLoad中有代码。 self.playedGames和self.messages也是我通过查询另外两个类得到的NSArrays。
newArray = [[NSMutableArray alloc]initWithCapacity:[self.playedGames count]];
messagesArray = [[NSMutableArray alloc]initWithCapacity:[self.messages count]];
ImageArray = [[NSMutableArray alloc]initWithCapacity:[self.playedGames count]];
PFQuery *query = [PFUser query];
[query orderByDescending:@"username"];
NSArray *Userarray1 = [query findObjects];
//NSLog(@"Userarray1 = %@",Userarray1);
PFFile *imageFile = [[PFFile alloc]init];
NSURL *imageFileUrl = [[NSURL alloc]init];
NSData *imageData = [[NSData alloc]init];
for(id i in Userarray1){
userPerson = [i valueForKey:@"username"];
for(id object in self.messages){
if([[object objectForKey:@"senderName"]isEqualToString:userPerson]){
imageFile = [i objectForKey:@"profilePic"];
if(imageFile !=nil){
imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
imageData = [NSData dataWithContentsOfURL:imageFileUrl];
newImageset = [UIImage imageWithData:imageData];
PFImageView *imgVew = [[PFImageView alloc] initWithFrame:CGRectMake(4, 5, 50, 50)];
imgVew.image = newImageset;
imgVew.opaque = YES;
[messagesArray addObject:imgVew];
}
else{
UIImage *NoPP = [UIImage imageNamed:@"friends_tab.png"];
NoPP = [self imageScaledToSize:CGSizeMake(50, 50)];
PFImageView *NoPPView = [[PFImageView alloc]initWithFrame:CGRectMake(4, 5, 50, 50)];
NoPPView.image = NoPP;
NoPPView.opaque = YES;
[messagesArray addObject:NoPPView];
}
}
}
//step two
for(id object in self.playedGames){
if([[object objectForKey:@"yourName"] isEqualToString:userPerson]){
imageFile = [i objectForKey:@"profilePic"];
if(imageFile !=nil){
imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
imageData = [NSData dataWithContentsOfURL:imageFileUrl];
newImageset = [UIImage imageWithData:imageData];
PFImageView *imgVew = [[PFImageView alloc] initWithFrame:CGRectMake(4, 5, 50, 50)];
imgVew.image = newImageset;
imgVew.opaque = YES;
[ImageArray addObject:imgVew];
//NSLog(@"ImageArray = %@",ImageArray);
}
else{
UIImage *NoPP = [UIImage imageNamed:@"friends_tab.png"];
NoPP = [self imageScaledToSize:CGSizeMake(50, 50)];
PFImageView *NoPPView = [[PFImageView alloc]initWithFrame:CGRectMake(4, 5, 50, 50)];
NoPPView.image = NoPP;
NoPPView.opaque = YES;
[ImageArray addObject:NoPPView];
}
}
}
}
答案 0 :(得分:0)
两个非常大的问题。
[query findObjects];
是同步的,这意味着它在完成之前不会返回。这需要在后台进行。看一下Parse提供的findInBackground方法之一。imageData = [NSData dataWithContentsOfURL:imageFileUrl];
从URL加载图像资源。同样,这是同步的,将等到一个图像加载后再继续下一个图像。这些图像加载也需要异步。答案 1 :(得分:0)
解决它并添加了这个
NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
newImageset = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
PFImageView *imgVew = [[PFImageView alloc] initWithFrame:CGRectMake(4, 5, 50, 50)];
imgVew.image = newImageset;
imgVew.opaque = YES;
[messagesArray addObject:imgVew];
});
loadingthesearches1 = YES;
[self ReloadTheData];
});
它的工作速度提高了90%