Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Trying to answer your main question "How can i load images to a <code>UICollectionview</code> asynchronously?"</p> <p>I would suggest solution offered by "<em>Natasha Murashev</em>" <a href="http://natashatherobot.com/ios-how-to-download-images-asynchronously-make-uitableview-scroll-fast/" rel="nofollow">here</a>, which worked nicely for me and it's simple.</p> <p>If here <code>imgURL = [allImage objectAtIndex:k];</code> in <code>allImage</code> property you keep array of URLs, then update your <code>collectionView:cellForItemAtIndexPath:</code> method like this:</p> <pre><code>- (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]; } }]; } </code></pre> <p>And add method <code>downloadImageWithURL:completionBlock:</code> to your class, which will load images asynchronously and update cells in CollectionView automatically when images are successfully downloaded. </p> <pre><code>- (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); } }]; } </code></pre> <p>I see you try to preload images before view appears so maybe my solution isn't what you what, but from your question it's hard to say. Any how, you can achieve what you want with this as well.</p> <p><strong>Swift 2.2</strong> Solution in Swift.</p> <pre><code>public typealias ImageFetchCompletionClosure = (image: UIImage?, error: NSError?, imageURL: NSURL?) -&gt; Void extension String { func fetchImage(completionHandler: (image: UIImage?, error: NSError?, imageURL: NSURL?) -&gt; 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()) { () -&gt; Void in completionHandler(image: image, error: nil, imageURL: imageURL) } }.resume() } } } </code></pre> <p>Usage sample:</p> <pre><code> "url_string".fetchImage { (image, error, imageURL) in // handle different results, either image was downloaded or error received } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload