
以下代码从服务器获取产品图像,并等待下载所有图像,然后在单元格中显示它们.以下代码有效,但用户等待10-20秒才能看到所有产品.有更好的办法吗?
- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; dispatch_queue_t queue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_HIGH,0ul); dispatch_async(queue,^(voID) { [self loadFromURL]; dispatch_async(dispatch_get_main_queue(),^{ }); });}-(voID)loadFromURL { NSURL *url = [NSURL URLWithString:@"http://myURL/productAll.PHP"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFhttpRequestoperation *operation = [[AFhttpRequestoperation alloc] initWithRequest:request]; operation.responseSerializer = [AFJsONResponseSerializer serializer]; [operation setCompletionBlockWithSuccess:^(AFhttpRequestoperation *operation,ID responSEObject) { pElements= (NSMutableArray *)responSEObject; [collectionVIEw reloadData]; } failure:^(AFhttpRequestoperation *operation,NSError *error) { UIAlertVIEw *alertVIEw = [[UIAlertVIEw alloc] initWithTitle:@"Error RetrIEving Product" message:[error localizedDescription]delegate:nil cancelbuttonTitle:@"Ok" otherbuttonTitles:nil]; [alertVIEw show]; }]; [operation start];}- (NSInteger)collectionVIEw:(UICollectionVIEw *)collectionVIEw numberOfItemsInSection:(NSInteger)section{ return pElements.count;}- (UICollectionVIEwCell *)collectionVIEw:(UICollectionVIEw *)collectionVIEw cellForItemAtIndexPath:(NSIndexPath *)indexPath{ ProductCell *cell=[collectionVIEw dequeueReusableCellWithReuseIDentifIEr:@"cellIDentifIEr" forIndexPath:indexPath]; cell.backgroundcolor=[UIcolor whitecolor]; Nsstring *arrayResult = [[pElements objectAtIndex:indexPath.row] objectForKey:@"image"]; NSData *data = [[NSData alloc] initWithBase64EncodedString:arrayResult options:NSDataBase64DeCodingIgnoreUnkNownCharacters]; cell.productimage.image = [[UIImage alloc] initWithData:data]; cell.productimage.layer.cornerRadius = 10; cell.productimage.clipsToBounds = YES; return cell;}解决方法 您有来自服务器的响应,其中所有图像的图像数据在响应中以base-64编码.这意味着响应可能非常大,并且在下载所有内容之前不会向用户显示. 相反,您可以考虑重构您的服务器代码,使其不包含base-64格式的图像数据,而是仅包含可用于稍后检索图像的URL(或某些标识符).您的响应应该更小,并且应该能够更快地处理.
然后,当调用cellForItemAtIndexPath时,不是从原始响应中提取图像数据,而是懒洋洋地(并且异步地)请求单元格的图像. AFNetworking在UIImageVIEw AFNetworking中提供了一个很好的UIImageVIEw类别,可以从网络源异步检索图像. (使用此类别可以让您在进行异步图像检索时摆脱许多微妙问题的杂草.)
顺便说一句,如果您的图像大小不同,您可能希望在原始请求中包含图像的尺寸,以便可以在前面适当调整单元格及其图像视图的大小,而不是在检索图像时调整它们的大小.
–
几点意见:
>您不需要将[self loadFromURL]调度到后台队列,因为它已经是异步的.我可能会使用请求的GET
>您不能将responSEObject强制转换为NSMutableArray,因为它可能不可变.如果你真的需要它是可变的,你真的应该使用NSArray或使用mutablecopy.
>您正在cellForItemAtIndexPath中进行一些单元格配置.大多数(剪辑,背景颜色等)可以在IB中完成,所以我会在那里做,而不是以编程方式进行.您可能需要以编程方式执行的唯一 *** 作是对角的舍入(即使这样,我可能会使用IBDesignable子类,尽管这超出了此问题的范围).
因此,假设(a)您的数组有一个名为imageURL的新属性,它是图像的URL; (b)单元格具有固定大小的图像视图,您可以执行以下 *** 作:
@interface VIEwController ()@property (nonatomic,strong) AFhttpRequestoperationManager *manager;@property (nonatomic,strong) NSArray *pElements;@end@implementation VIEwController- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; self.manager = [AFhttpRequestoperationManager manager]; [self loadFromURL];}-(voID)loadFromURL { Nsstring *urlString = @"http://myURL/productAll.PHP"; [self.manager GET:urlString parameters:nil success:^(AFhttpRequestoperation * _Nonnull operation,ID _Nonnull responSEObject) { self.pElements = responSEObject; [self.collectionVIEw reloadData]; } failure:^(AFhttpRequestoperation * _Nullable operation,NSError * _Nonnull error) { UIAlertVIEw *alertVIEw = [[UIAlertVIEw alloc] initWithTitle:@"Error RetrIEving Product" message:[error localizedDescription]delegate:nil cancelbuttonTitle:@"Ok" otherbuttonTitles:nil]; [alertVIEw show]; }];}- (NSInteger)collectionVIEw:(UICollectionVIEw *)collectionVIEw numberOfItemsInSection:(NSInteger)section { return self.pElements.count;}- (UICollectionVIEwCell *)collectionVIEw:(UICollectionVIEw *)collectionVIEw cellForItemAtIndexPath:(NSIndexPath *)indexPath { ProductCell *cell = [collectionVIEw dequeueReusableCellWithReuseIDentifIEr:@"cellIDentifIEr" forIndexPath:indexPath]; Nsstring *imageURL = self.pElements[indexPath.row][@"imageURL"]; [cell.productimage setimageWithURL:[NSURL URLWithString:imageURL]]; cell.productimage.layer.cornerRadius = 10; return cell;}@end 总结 以上是内存溢出为你收集整理的ios – 从服务器下载图像以显示在CollectionView上全部内容,希望文章能够帮你解决ios – 从服务器下载图像以显示在CollectionView上所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)