Skip to content

Instantly share code, notes, and snippets.

@0xjmp
Created April 21, 2014 21:43
Show Gist options
  • Select an option

  • Save 0xjmp/11157587 to your computer and use it in GitHub Desktop.

Select an option

Save 0xjmp/11157587 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
typedef void (^DDURLCompletionBlock)(NSData *responseData, NSError *error);
@interface DDURLConnection : NSURLConnection
- (id)initWithRequest:(NSURLRequest *)request startImmediately:(BOOL)startImmediately completion:(DDURLCompletionBlock)completionBlock;
@end
#import "DDURLConnection.h"
@interface DDURLConnection () <NSURLConnectionDataDelegate>
@property (strong, nonatomic) DDURLCompletionBlock completionBlock;
@end
@implementation DDURLConnection
- (void)dealloc
{
self.completionBlock = nil;
}
- (id)initWithRequest:(NSURLRequest *)request startImmediately:(BOOL)startImmediately completion:(DDURLCompletionBlock)completionBlock;
{
self = [super initWithRequest:request delegate:self startImmediately:startImmediately];
if (self)
{
if (completionBlock)
self.completionBlock = completionBlock;
}
return self;
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"connection: %@ didFailWithError: %@", connection, error);
if (self.completionBlock) self.completionBlock(nil, error);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (self.completionBlock) self.completionBlock(data, nil);
}
@end
#import "DDViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "DDURLConnection.h"
@interface DDViewController () <UITableViewDataSource, UITableViewDelegate, NSURLConnectionDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *photo_urls;
@end
@implementation DDViewController
@synthesize photo_urls;
- (void)dealloc
{
photo_urls = nil;
}
- (id)init
{
self = [super init];
if (self)
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
NSMutableArray *urls = [NSMutableArray arrayWithCapacity:20];
for (int i = 0; i < 25; i++)
{
[urls addObject:@"https://d2pnn7clgn6qxc.cloudfront.net/assets/products/mens/jeans/swatch/raw-premium/indigo-natural.jpg"];
}
photo_urls = urls;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tableView.frame = self.view.bounds;
}
#pragma mark - Getters
- (NSInteger)imageViewTag
{
return 1415;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return photo_urls.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 320;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MemeCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MemeCell"];
}
// Dequeue subviews
UIImageView *imageView = (UIImageView *)[cell viewWithTag:[self imageViewTag]];
if (!imageView)
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
imageView.tag = [self imageViewTag];
[cell addSubview:imageView];
}
NSInteger activityTag = 54321;
if (!imageView.image && ![cell viewWithTag:activityTag])
{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.tag = activityTag;
activityIndicator.center = cell.center;
activityIndicator.hidesWhenStopped = YES;
[activityIndicator startAnimating];
[cell addSubview:activityIndicator];
// No image set... fetch it on a background thread & set it dyanmically (weak reference).
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[photo_urls objectAtIndex:indexPath.row]]];
__block DDURLConnection *openConnection = [[DDURLConnection alloc] initWithRequest:request startImmediately:YES completion:^(NSData *responseData, NSError *error)
{
[activityIndicator stopAnimating];
UIImage *image = [[UIImage alloc] initWithData:responseData];
if (!error && image)
{
imageView.image = image;
// Removing activityIndicator from it's superview will reset the cell's -viewWithTag: results.
// The imageView's image is set so we don't need to make another DDURLConnection
[activityIndicator removeFromSuperview];
}
openConnection = nil; // Dealloc
}];
}
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment