使用ALAssetLibrary

时间:2016-04-14 12:10:55

标签: ios

我正在尝试使用ALAssetLibrary从图库到集合视图中获取视频列表。但视频不会在集合视图中显示。

得到错误:

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无法使类型的视图出列:具有标识符cellIdentifier的UICollectionElementKindCell - 必须为标识符注册一个nib或类或连接一个故事板中的原型单元' ***第一次抛出调用堆栈: (0x2ba0749f 0x395fbc8b 0x2ba07375 0x2c6d8d7f 0x2f4cb573 0x2ef26cd1 0x580d7 0x2ef268f5 0x2ef24fc1 0x2ef20c09 0x2eec724f 0x2e8efa0d 0x2e8eb3e5 0x2e8eb26d 0x2e8eac51 0x2e8eaa55 0x2e8e492d 0x2b9cdd95 0x2b9cb453 0x2b9cb85b 0x2b9193c1 0x2b9191d3 0x32cfd0a9 0x2ef28fa1 0x6de09 0x39b7baaf) libc ++ abi.dylib:以NSException类型的未捕获异常终止

请帮我解决这个问题。

我的代码在这里 -

在viewDidLoad()中使用此代码创建集合视图:

self.view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[collectionView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:collectionView];

像这样加载资产 -

loadAssets()
{
allVideos = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
 {
     if (group)
     {
         [group setAssetsFilter:[ALAssetsFilter allVideos]];
         [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
          {
              if (asset)
              {
                  dic = [[NSMutableDictionary alloc] init];
                  ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
                  NSString *uti = [defaultRepresentation UTI];
                  NSURL  *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
                  NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100];
                  UIImage *image = [self imageFromVideoURL:videoURL];
                  [dic setValue:image forKey:@"image"];
                  [dic setValue:title forKey:@"name"];
                  [dic setValue:videoURL forKey:@"url"];
                  [allVideos addObject:asset];

              }
          }];
         [_collectionView reloadData];
     }
 }
                          failureBlock:^(NSError *error)
{
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
}
- (UIImage *)imageFromVideoURL:(NSURL*)videoURL
{

UIImage *image = nil;
AVAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];;
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;

// calc midpoint time of video
Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);

// get the image from
NSError *error = nil;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];

if (halfWayImage != NULL)
{
    // cgimage to uiimage
    image = [[UIImage alloc] initWithCGImage:halfWayImage];
    [dic setValue:image forKey:@"ImageThumbnail"];//kImage
    NSLog(@"Values of dictonary==>%@", dic);
    NSLog(@"Videos Are:%@",allVideos);
    CGImageRelease(halfWayImage);
}
return image;
}

-(void)viewDidAppear:(BOOL)animated
{
[self loadAssets];

}

在集合视图中 -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 {
return [allVideos count];
 }

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

NSLog(@"allvideo %@", allVideos);
ALAsset *alasset = [allVideos objectAtIndex:indexPath.row];

return cell;
 }

0 个答案:

没有答案