我正在尝试从服务器获取数据并使用该数据填充集合视图单元格,但是当集合视图被填充时会冻结并且需要很长时间才能滚动。
究竟出了什么问题?
-(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];
});
}];
}
收藏视图
-(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;
}
}
内部集合视图单元
-(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];
}
}
答案 0 :(得分:2)
您似乎正在从网络加载图片(即从recipeResponseArr["user"]["image"]
主线程中通过initWithContentsOfURL:加载图片。强烈建议您不要采用最佳实践。来自文档:
重要
请勿使用此同步方法来请求基于网络的网址。对于 基于网络的URL,此方法可以阻止当前线程为数十 慢速网络上的秒数,导致用户体验不佳,以及 在iOS中,可能会导致您的应用被终止。
相反,对于非文件网址,请考虑使用 dataTaskWithURL:completionHandler:NSURLSession类的方法。 有关详细信息,请参阅URL会话编程指南。
如果您需要有效且简单的工具来加载图片,我建议您使用SDWebImage或AlamofireImage