gpt4 book ai didi

ios - 使用通过点击 API 获得的响应进行填充时, Collection View 会卡住很长时间

转载 作者:行者123 更新时间:2023-11-28 21:02:11 25 4
gpt4 key购买 nike

我正在尝试从服务器获取数据并使用该数据填充 Collection View 单元格,但是当 Collection View 被填充时卡住并且需要很长时间才能滚动。

到底出了什么问题?

-(void)callService:(NSNumber*)categoryid{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];

[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
NSMutableDictionary *dict = [NSMutableDictionary new];
[dict setValue:@1 forKey:@"page"];
[dict setValue:@25 forKey:@"limit"];

[dict setValue:categoryid forKey:@"category_id"];

[manager POST:@"https://www.foodfuels.com/Api/getrecipes"
parameters:dict progress:nil success:^(NSURLSessionTask *task,id responseObject)
{
int status = [[responseObject objectForKey:@"status"] intValue];

if(status == 200)
{
responseArray = [responseObject valueForKey:@"data"];

dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:true];
[_outerCollectionView reloadData];

});
}

else
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"logininfo"];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"some eror!" delegate:self cancelButtonTitle:Nil otherButtonTitles:@"Ok", nil];
[alert show];
}
dispatch_async(dispatch_get_main_queue(), ^{

//[MBProgressHUD hideHUDForView:self.view animated:YES];

});

}
failure:^(NSURLSessionTask *operation, NSError *error)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:self cancelButtonTitle:Nil otherButtonTitles:@"Ok", nil];

[alert show];

dispatch_async(dispatch_get_main_queue(), ^{

// [MBProgressHUD hideHUDForView:self.view animated:YES];

});

}];

}

Collection View

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if(collectionView==_headingCollectionView){
recipeHeading = (RecipeHeadingCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"headingCell" forIndexPath:indexPath];
_headingCollectionView.delegate = self;
[recipeHeading.headingBtn setTitle:[recipeTypeArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[recipeHeading.headingBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];

bool d = [allValues[indexPath.row] boolValue];

if(d)
{
[recipeHeading.headingBtn setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal]; [recipeHeading.headingBtn addSubview:bottomBorder];
}
else
{
[recipeHeading.headingBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
}

return recipeHeading;
}
else if (collectionView==_outerCollectionView){
RecipeOuterCell *outerCell =(RecipeOuterCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"outerCell" forIndexPath:indexPath];
return outerCell;
}
else{
RecipeInnerCell *innerCell = (RecipeInnerCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"innerCell" forIndexPath:indexPath];
[innerCell populateRecipeScreen:responseArray index:indexPath];
return innerCell;

}
}

内部 Collection View 单元格

-(void)populateRecipeScreen:(NSArray*)recipeResponseArr index:(NSIndexPath*)path{
NSLog(@"the value of recipe array are as follows %@",recipeResponseArr);
for(int i=0;i<recipeResponseArr.count;i++){
self.comment_Count.text =[[[recipeResponseArr valueForKey:@"comment_count"]objectAtIndex:path.row]stringValue];
self.titleLbl.text =[[recipeResponseArr valueForKey:@"description"]objectAtIndex:path.row];
self.like_Count.text = [[[recipeResponseArr valueForKey:@"like_count"]objectAtIndex:path.row]stringValue];
NSData *data= [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [[[recipeResponseArr valueForKey:@"user"]valueForKey:@"image"] objectAtIndex:path.row]]];
; self.smallImgView.image=[UIImage imageWithData:data];

self.share_Count.text = [[[recipeResponseArr valueForKey:@"share_count"]objectAtIndex:path.row]stringValue];
self.subTitleLbl.text = [[[recipeResponseArr valueForKey:@"user"]valueForKey:@"username"] objectAtIndex:path.row];
NSLog(@"url obtained as result %@",[[[recipeResponseArr valueForKey:@"upload_images"]valueForKey:@"name"] objectAtIndex:path.row]);
NSArray *urlString = [[[recipeResponseArr valueForKey:@"upload_images"]valueForKey:@"name"] objectAtIndex:path.row];

NSData *mainData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[urlString objectAtIndex:0]]];
self.mainImg.image=[UIImage imageWithData:mainData];
}
}

最佳答案

您似乎正在通过 initWithContentsOfURL: 在主线程中从网络加载图像(即从 recipeResponseArr["user"]["image"] 加载图像。强烈建议不要这样做,并且最好不要这样做。来自文档:

Important

Don't use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSURLSession class. See URL Session Programming Guide for details.

如果您需要有效且简单的工具来加载图像,我建议您使用 SDWebImageAlamofireImage

关于ios - 使用通过点击 API 获得的响应进行填充时, Collection View 会卡住很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47953496/

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