我有两个数组,一个是用户名,另一个是玩过的游戏,我想将用户名数组中的个人资料图片与玩游戏中的yourName相匹配,但我似乎无法让它工作。
for(id object in self.playedGames){
PFObject *obb = object;
for(int l = 0; l <self.Userarray1.count;l++){
if([[obb objectForKey:@"yourName"]isEqual:[[self.Userarray1 valueForKey:@"username"]objectAtIndex:l]] ){
PFFile *imageFile = [[self.Userarray1 valueForKey:@"profilePic"]objectAtIndex:l];
if(imageFile !=nil){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSURL* imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
UIImage *newImageset = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
UIImageView *imgVew = [[UIImageView alloc] initWithFrame:CGRectMake(4, 5, 50, 50)];
imgVew.image = newImageset;
imgVew.opaque = YES;
[ImageArray addObject:imgVew];
NSLog(@"imageArray count = %i",ImageArray.count);
if(ImageArray.count == self.playedGames.count){
[self getStuff];
}
});
});
}
else{
UIImage *NoPP = [UIImage imageNamed:@"friends_tab.png"];
NoPP = [self imageScaledToSize:CGSizeMake(50, 50)withImage:NoPP];
UIImageView *NoPPView = [[UIImageView alloc]initWithFrame:CGRectMake(4, 5, 50, 50)];
NoPPView.image = NoPP;
NoPPView.opaque = YES;
[ImageArray addObject:NoPPView];
}
}
}
}
答案 0 :(得分:0)
意识到我正在进行异步工作和同步工作,所以ImageArray在不同时间被添加,所以我用这个解决方案修复它
if(imageFile !=nil){
UIImageView *imgVew = [[UIImageView alloc]initWithFrame:CGRectMake(4, 5, 50, 50)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSURL* imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
UIImage *newImageset = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
imgVew.image = newImageset;
imgVew.opaque = YES;
});
});
[ImageArray addObject:imgVew];
NSLog(@"imageArray count = %i",ImageArray.count);
if(ImageArray.count == self.playedGames.count){
[self getStuff];
}
}
它也使得更快地遍历该功能