gpt4 book ai didi

ios - 使用 ALAssetLibrary 从图库加载视频时未获得 Assets 值(value)

转载 作者:行者123 更新时间:2023-11-29 12:01:46 24 4
gpt4 key购买 nike

我想通过使用 ALAssetLibrary 显示视频的缩略图,为了将图库中的视频显示到我的应用程序,我从 ALAssetsFilter 中过滤了所有视频。

但我仍然在 ALAsset 类型的 Assets 中得到空值。

请告诉我我的代码做错了什么。

感谢您的帮助。

-(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;
}

最佳答案

使用此代码可能会帮助您生成缩略图 -- -

-(UIImage*)loadImage {



AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.videoURL options:nil];



AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];



generate.appliesPreferredTrackTransform = YES;



NSError *err = NULL;



CMTime time = CMTimeMake(1, 60);



CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];



return [[UIImage alloc] initWithCGImage:imgRef];



}

关于ios - 使用 ALAssetLibrary 从图库加载视频时未获得 Assets 值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com