我有一个CollectionView,在CollectionView单元格中,它有一个UIImageView。 我使用AFNetworking从Web服务加载图像。但是加载数据需要花费更多的时间。有什么办法可以加快这个过程。我附上了我的代码。 希望你的帮助。
//加载数据
- (void)loadcategoryData
{
post = nil;
NSString *mainurl = [NSString stringWithFormat:@"some url"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager GET:mainurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
posts = (NSDictionary *)responseObject;
post = [NSMutableArray array];
for(NSDictionary *all in posts)
{
Categories *category = [Categories new];
category.title = [all objectForKey:@"catname"];
category.firsturl = [all objectForKey:@"url"];
[self.maincollectionView reloadData];
//call for images
imagespost = nil;
NSString *imageUrl = [NSString stringWithFormat:@"%@", category.firsturl];
AFHTTPRequestOperationManager *managerone = [AFHTTPRequestOperationManager manager];
[managerone GET:imageUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
imagesposts = (NSDictionary *)responseObject;
NSArray *resultone = [imagesposts objectForKey:@"posts"];
imagespost = [NSMutableArray array];
if ([resultone count]) {
NSDictionary *firstpost = resultone[0];
// Categories *newmogocat = [Categories new];
NSDictionary *thumbnail_images = [firstpost objectForKeyedSubscript:@"thumbnail_images"];
NSDictionary *thumbnail = [thumbnail_images objectForKey:@"thumbnail"];
category.imageOneUrl = [NSString stringWithFormat:@"%@",[thumbnail objectForKey:@"url"]];
// NSLog(@"%@", newmogocat.imageOneUrl);
// [imagespost addObject:newmogocat];
[post addObject:category];
[self.maincollectionView reloadData];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
} failure:^(AFHTTPRequestOperation *operation, NSError * responseObject) {
}];
}
//将值分配给collectionview单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellidentifier" forIndexPath:indexPath];
// cell.layer.borderWidth = 2.0f;
cell.layer.masksToBounds = NO;
cell.layer.cornerRadius = 5.0;
cell.layer.borderColor = [UIColor purpleColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 1);
cell.layer.shadowRadius = 2.0;
cell.layer.shadowColor = [UIColor blueColor].CGColor;
[cell addSubview:cell.maintitleLabel];
Categories *cat = [post objectAtIndex:indexPath.row];
[self.maincollectionView reloadInputViews];
cell.maintitleLabel.text = [NSString stringWithFormat:@" %@ ", cat.title];
[cell.maintitleLabel sizeToFit];
NSString *mainstring = [NSString stringWithFormat:@"%@", cat.imageOneUrl];
NSURL *url = [NSURL URLWithString:mainstring];
// NSLog(@"%@", mainstring);
[cell.mainImageView setImageWithURL:url placeholderImage:nil];
return cell;
}
答案 0 :(得分:0)
导入“UIImage + AfNetworking.h”
内部- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:cat.imageOneUrl]];
[EDIT] :
__weak CategoryCollectionViewCell *weakCell = cell; [EDIT]
[cell.articleThumbnail setImageWithURLRequest:req placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[EDIT] weakCell.articleThumbnail.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
[EDIT] weakCell.articleThumbnail.image = [UIImage imageNamed:@"placeholder.png"];
}];
希望这有帮助