テーブルビューにカメラロールから画像を取り込もうとしています。
でViewDidLoad
、アセット グループを作成します。
- (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];
}
ではCellForRowAtIndexPath
、GCD バックグラウンド キューを使用して、セルにそれぞれのインデックスの画像を入力します。
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;
問題は、最初のセルが空であることです。スクロールを開始した後にのみ、画像の読み込みが開始されます。それも、スクロールするとすぐにアプリがクラッシュします。私はGCDにかなり慣れていないので、正しく使用していないようです。この問題の助けをいただければ幸いです。