本文實例為大家分享了iOS視頻錄像切成縮略圖的具體代碼,供大家參考,具體內容如下
記得導入系統庫
#import < MediaPlayer/MediaPlayer.h >
代碼:
/** * 獲取網絡視頻的全部縮略圖方法 * * @param videoURL 視頻的鏈接地址 * * @return 視頻截圖 */ + (UIImage *)ihefe_previewImageWithVideoURL:(NSURL *)videoURL { AVAsset *asset = [AVAsset assetWithURL:videoURL]; AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset]; generator.appliesPreferredTrackTransform = YES; CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(1, asset.duration.timescale) actualTime:NULL error:nil]; UIImage *image = [UIImage imageWithCGImage:img]; CGImageRelease(img); return image; } /** * 獲取本地視頻的全部縮略圖方法 * * @param fileurl 視頻的鏈接地址 * * @return 視頻截圖 */ + (UIImage *)ihefe_getScreenShotImageFromVideoURL:(NSString *)fileurl { UIImage *shotImage; //視頻路徑URL NSURL *fileURL = [NSURL URLWithString:fileurl]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL 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]; shotImage = [[UIImage alloc] initWithCGImage:image]; CGImageRelease(image); return shotImage; } /** * 獲取視頻的某一幀縮略圖方法 * * @param videoURL 視頻的鏈接地址 幀時間 * @param time 幀時間 * * @return 視頻截圖 */ + (UIImage*)ihefe_thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time { AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; NSParameterAssert(asset); AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; assetImageGenerator.appliesPreferredTrackTransform = YES; assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels; CGImageRef thumbnailImageRef = NULL; CFTimeInterval thumbnailImageTime = time; NSError *thumbnailImageGenerationError = nil; thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError]; if (!thumbnailImageRef) NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError); UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil; return thumbnailImage; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。