ALAssetLibrary
を使用してビデオのサムネイルを表示し、ギャラリーからアプリにビデオを表示するために、ALAssetsFilter
からすべてのビデオをフィルタしました。ALAssetLibraryを使用してギャラリーからビデオをロードするときにアセットの値を取得しない
しかし、私はまだタイプALAsset
の資産でnull値を取得しています。
私のコードで間違っていることを教えてください。
助けをお待ちしております。
私はビデオからサムネイル画像を作成してい-(void)loadAssets{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *innerStop) {
if (asset)
{
dic = [[NSMutableDictionary alloc] init];
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
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;
}
私はこれを使用しましたが、エラーを取得しています:所有しているALAssetsLibraryの存続期間を過ぎてにアクセスしようとすると、無効な試みが発生しました –
ビデオURLは何ですか?あなたのコードでAVAssetImageGeneratorにAVAssetを渡しています。 AVURLAsset *アセットを渡すか初期化してみてください。このコードはうまく動作します。 –
あなたはまた、エラー解決のためにこれを試すことができます - http://stackoverflow.com/questions/22662464/alassetslibrary-invalid-or-null –