从库中提取的视频的 UIImagePickerController 缩略图
Posted
技术标签:
【中文标题】从库中提取的视频的 UIImagePickerController 缩略图【英文标题】:UIImagePickerController thumbnail of video which is pick up from library 【发布时间】:2011-08-08 19:33:16 【问题描述】:我正在尝试使用 UIImagePickerController 从库中获取视频的缩略图。
这是我的代码。如果我使用相机录制视频,我可以获得视频的缩略图。但是,如果我从图书馆拿起视频,我就无法获得缩略图。有人知道为什么吗?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [[moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame] retain];
[imageView setImage:thumbnail]; //imageView is a UIImageView
[moviePlayer release];
[thumbnail release];
[self dismissModalViewControllerAnimated:YES];
【问题讨论】:
【参考方案1】:我遇到了一个半相关的问题,最终放弃了使用 MPMoviePlayer 生成缩略图。尝试改用AVAssetImageGenerator。 Apple 讨论使用 AVAssetImageGenerator 创建缩略图here。这是我自己的示例代码,它抓取一个缩略图。您将需要包含 AVFoundation 框架。
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidPath options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
[gen release];
【讨论】:
【参考方案2】:AssetsLibrary 框架具有您所需要的 ALAssetsLibrary
和 ALAsset
类。此代码适用于照片和视频,缩略图与您在选择器中看到的完全相同。
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
[[ALAssetsLibrary new] assetForURL:info[UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset)
imageView.image = [UIImage imageWithCGImage:asset.thumbnail];
failureBlock:^(NSError *error)
// handle error
];
[self dismissViewControllerAnimated:YES completion:nil];
请注意,ios 8 上的 Photo Stream 相册存在一个错误,请参阅ALAssetsLibrary assetForURL: always returning nil for photos in “My Photo Stream” in iOS 8.1 了解解决方法。
【讨论】:
在 iOS 8 及更高版本上,Apple 建议使用 Photos framework 而不是 ALAssetsLibrary。这将是比回退到enumerateGroupsWithTypes:usingBlock:
更好的方法。
我认为该评论属于链接问题。【参考方案3】:
func videoSnapshot(filePathLocal:URL) -> UIImage?
do
let asset = AVURLAsset(url: filePathLocal)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at:CMTimeMake(Int64(0), Int32(1)),actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
catch let error as NSError
print("Error generating thumbnail: \(error)")
return nil
【讨论】:
解决问题【参考方案4】:Swift 5及以上
func thumbnailForVideo(url: URL) -> UIImage?
let asset = AVAsset(url: url)
let assetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImageGenerator.appliesPreferredTrackTransform = true
var time = asset.duration
time.value = min(time.value, 2)
do
let imageRef = try assetImageGenerator.copyCGImage(at: time, actualTime: nil)
return UIImage(cgImage: imageRef)
catch
print("failed to create thumbnail")
return nil
【讨论】:
【参考方案5】:斯威夫特 3.1
我已经创建了一个类函数并在参数中传递了你的 url
class func captureThumbnail(withVideoURL videoURL:URL,secs:Int,preferredTimeScale scale:Int,completionHandler:((UIImage?) ->Void)?) -> Void
//let seconds : Int64 = 10
// let preferredTimeScale : Int32 = 1
DispatchQueue.global().async
do
let asset = AVURLAsset(url: videoURL)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at:CMTimeMake(Int64(secs), Int32(scale)),actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
completionHandler?(thumbnail)
catch let error as NSError
print("Error generating thumbnail: \(error)")
completionHandler?(nil)
【讨论】:
以上是关于从库中提取的视频的 UIImagePickerController 缩略图的主要内容,如果未能解决你的问题,请参考以下文章
从库中选择视频时未调用 uiimagepickercontroller didfinishpickingmediawithinfo