如何从 iOS 9 和 Objective C 中的 .MOV 文件中获取缩略图?
Posted
技术标签:
【中文标题】如何从 iOS 9 和 Objective C 中的 .MOV 文件中获取缩略图?【英文标题】:How to get the thumbnail from .MOV file in iOS 9 and Objective C? 【发布时间】:2016-07-24 17:51:12 【问题描述】:选择或拍摄视频后,我想在下面的同伴代表旁边使用该视频的缩略图更新我的UIUImageView
。我如何使用PHAsset
for ios 9 和Objective C
来做到这一点?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
【问题讨论】:
【参考方案1】:尝试使用下面的代码,其中asset
参数是您的PHAsset
实例:
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestImageForAsset: asset
targetSize: CGSizeMake(100.0, 100.0) // use required size
contentMode: PHImageContentModeAspectFill
options: nil
resultHandler: ^(UIImage * _Nullable anImage, NSDictionary * _Nullable info)
dispatch_async(dispatch_get_main_queue(), ^
yourImageView.image = anImage;
);
];
【讨论】:
【参考方案2】:- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
self.sourceURLString =[NSString stringWithFormat:@"%@", [info objectForKey:UIImagePickerControllerMediaURL]];
dispatch_async(dispatch_get_main_queue(), ^
self.thumbImageView.image = [self generateThumbImage:self.sourceURLString];
self.addVideoButton.hidden = YES;
);
-(UIImage *)generateThumbImage : (NSString *)sourceURLString
NSURL *sourceMovieURL = [NSURL URLWithString:sourceURLString];
AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:sourceAsset];
NSError *error = NULL;
CMTime time = CMTimeMake(1, 65);
CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error];
NSLog(@"error==%@, Refimage==%@", error, refImg);
return [[UIImage alloc] initWithCGImage:refImg];
【讨论】:
如果你使用 PhotoLibrary,那么 -requestImageForAsset 可能是更好的方法。【参考方案3】: + (UIImage *)imageFromMovie:(NSURL *)movieURL atTime:(NSTimeInterval)time
// set up the movie player
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
initWithContentURL:movieURL];
mp.shouldAutoplay = NO;
mp.initialPlaybackTime = time;
mp.currentPlaybackTime = time;
// get the thumbnail
UIImage *thumbnail = [mp thumbnailImageAtTime:time
timeOption:MPMovieTimeOptionNearestKeyFrame];
// clean up the movie player
[mp stop];
[mp release];
return(thumbnail);
【讨论】:
MPMoviePlayerController 在 iOS 8 中已弃用以上是关于如何从 iOS 9 和 Objective C 中的 .MOV 文件中获取缩略图?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Objective C、iOS 中的 NSUserDefaults 获取准确数据?
如何从 AudioUnits C 函数调用 Objective-C 方法? (iOS)
如何在 iOS 8 Objective c 中的 UIWebView 中从文档中加载 png 图像
Objective C - NodeJS - 如何将特定文件(不是图像)从 iOS 应用程序上传到服务器?