Tôi đang cố gắng lấy khung hình đầu tiên từ video được chọn trong một UIImagePickerController
để hiển thị trong một số UIImageView
, nhưng tôi không biết nếu có thể. Nếu có, tôi sẽ làm như thế nào?Lấy khung hình đầu tiên của video từ UIImagePickerController?
Trả lời
Bạn có thể thực hiện việc này theo một trong hai cách. Cách đầu tiên là sử dụng MPMoviePlayerController
để lấy thumbnail:
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time
timeOption:MPMovieTimeOptionNearestKeyFrame];
này hoạt động, nhưng MPMoviePlayerController
không phải là một đối tượng đặc biệt là trọng lượng nhẹ và không đặc biệt nhanh grabbing hình thu nhỏ.
Cách ưu tiên là sử dụng AVAssetImageGenerator
mới trong AVFoundation. Đây là nhanh, nhẹ và linh hoạt hơn so với cách cũ. Đây là phương thức trợ giúp sẽ trả lại hình ảnh được tự động phát hành từ video.
+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL
atTime:(NSTimeInterval)time
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetIG =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *igError = nil;
thumbnailImageRef =
[assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
actualTime:NULL
error:&igError];
if (!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@", igError);
UIImage *thumbnailImage = thumbnailImageRef
? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
: nil;
return thumbnailImage;
}
sử dụng không đồng bộ
- (void)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time completion:(void (^)(UIImage *)) completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetIG =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *igError = nil;
thumbnailImageRef =
[assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
actualTime:NULL
error:&igError];
if (!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@", igError);
UIImage *thumbnailImage = thumbnailImageRef
? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
: nil;
dispatch_async(dispatch_get_main_queue(), ^{
completion(thumbnailImage);
});
});
}
Bạn cũng phải thêm khung "Core Media" vào mục tiêu của mình. –
+1 Đồng nghiệp của tôi không thể viết một cái gì đó như thế này, nhưng thx cho bạn ... –
cho những người khác cố gắng sử dụng điều này: chú ý nếu videoURL được tạo bởi fileURLWithPath hoặc URLWithString (khi bạn có đường dẫn trong NSString *) – user1105951
câu hỏi tương tự http://stackoverflow.com/questions/1347562/getting-thumbnail-from-a-video-url-or-data-in-iphone-sdk –