I'm trying to populate images from camera roll in a tableview.
in ViewDidLoad
, I create an asset group.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(nil!=group){
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
self.assetGroup = group;
NSLog(@"%d images found", self.assetGroup.numberOfAssets);
}
} failureBlock:^(NSError *error) {
NSLog(@"block fucked!");
}];
[self.tableView reloadData];
}
in CellForRowAtIndexPath
, I use GCD background queue to populate cells with image at respective index.
static NSString *CellIdentifier = @"Cell";
dispatch_queue_t imgLoadQueue = dispatch_queue_create("Thumb loader", NULL);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
dispatch_async(imgLoadQueue, ^{
[self.assetGroup enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:indexPath.row] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (nil != result) {
ALAssetRepresentation *repr = [result defaultRepresentation];
UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
cell.imageView.image = img;
}
}];
});
return cell;
The problem is, initial cells are empty. Images start loading only after I start scrolling. And that too, app crashes as soon as I scroll. I'm fairly new to GCD, and don't seem to be using it correctly. Any help in the matter is appreciated.