Last active
October 22, 2015 21:14
-
-
Save casspangell/99efb7cb09092bf2a6db to your computer and use it in GitHub Desktop.
Load Tableview Thumbs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Taken from and used with: | |
// http://stackoverflow.com/questions/16663618/async-image-loading-from-url-inside-a-uitableview-cell-image-changes-to-wrong | |
//////////////////// | |
//With SDWebImage // | |
//////////////////// | |
(don't use the below method. This one is king.) | |
//Import the goods and use PODS | |
// pod 'SDWebImage', '~> 3.7' | |
// | |
#import <SDWebImage/UIImageView+WebCache.h> | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
//Custom tableViewCell | |
static NSString *CellIdentifier = @"VideoCell"; | |
VideoCell *cell = (VideoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) | |
{ | |
cell = [[VideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
} | |
VideoObject *video = [VideoObject new]; | |
video = [self.safetyVideos objectAtIndex:indexPath.row]; | |
cell.titleLabel.text = video.videoTitle; | |
cell.videoURL = video.videoURL; | |
NSArray *components = [cell.videoURL componentsSeparatedByString:@"v="]; | |
if ([components count] != 0) { | |
cell.videoIdentifier = components[1]; | |
} | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_async(queue, ^{ | |
XCDYouTubeClient *client = [XCDYouTubeClient new]; | |
[client getVideoWithIdentifier:cell.videoIdentifier completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) { | |
//This is the money shot right here | |
[cell.thumbImageView sd_setImageWithURL:video.smallThumbnailURL placeholderImage:[UIImage imageNamed:@"youtube"]]; | |
}]; | |
}); | |
return cell; | |
} | |
////////////////////// | |
//Without SDWebImage// | |
////////////////////// | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ | |
//Using custom cell class myCell which has a UIImage thumbnail | |
myCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; | |
if (cell == nil) { | |
cell = [[myCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; | |
} | |
cell.thumbnail.image = nil; // or cell.poster.image = [UIImage imageNamed:@"placeholder.png"]; | |
//GDC queue | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_async(kBgQueue, ^{ | |
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://myurl.com/%@.jpg"]]]; | |
if (imgData) { | |
UIImage *image = [UIImage imageWithData:imgData]; | |
if (image) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
myCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath]; | |
if (updateCell) | |
updateCell.thumbnail.image = image; | |
}); | |
} | |
} | |
}); | |
return cell; | |
} | |
//Other example using YouTube API for grabbing thumbs | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
//Custom VideoCell class | |
static NSString *CellIdentifier = @"VideoCell"; | |
VideoCell *cell = (VideoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) | |
{ | |
cell = [[VideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
} | |
//VideoObject holds videoTitle, videoURL | |
VideoObject *video = [VideoObject new]; | |
video = [self.safetyVideos objectAtIndex:indexPath.row]; //Array of videos already loaded | |
cell.titleLabel.text = video.videoTitle; | |
cell.videoURL = video.videoURL; | |
//Grabbing the ID of the video from the URL | |
NSArray *components = [cell.videoURL componentsSeparatedByString:@"v="]; | |
if ([components count] != 0) { | |
cell.videoIdentifier = components[1]; | |
} | |
//Setting placeholder image | |
[cell.thumbImageView setImage:[UIImage imageNamed:@"youtube"]]; | |
//GDC queue | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_async(queue, ^{ | |
XCDYouTubeClient *client = [XCDYouTubeClient new]; | |
[client getVideoWithIdentifier:cell.videoIdentifier completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) { | |
//Setting everything up like in the above example | |
NSData *data = [NSData dataWithContentsOfURL:video.smallThumbnailURL]; //YouTube API has thumbs/image/etc for XCDYouTubeVideo objects | |
if (data) { | |
UIImage *image = [UIImage imageWithData:data]; | |
if (image) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
VideoCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath]; | |
if (updateCell) | |
updateCell.thumbImageView.image = image; | |
}); | |
} | |
} | |
}]; | |
}); | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment