iOS沙盒视频缩略图及保存本地代码

时间:2022-09-20 10:09:35

本文实例为大家分享了iOS沙盒视频缩略图及保存本地的具体代码,供大家参考,具体内容如下

获取缩略图

?
1
2
3
4
5
6
7
8
9
10
11
12
func thumbnailOfVideo(path : String)-> UIImage {
 do {
  let asset = AVURLAsset(url: URL(fileURLWithPath: path), options: nil)
  let imgGenerator = AVAssetImageGenerator(asset: asset)
  imgGenerator.appliesPreferredTrackTransform = true
  let cgImage = try imgGenerator.copyCGImage(at: CMTimeMakeWithSeconds(1, 24), actualTime: nil)
  let uiImage = UIImage(cgImage: cgImage)
  return uiImage
 } catch {
  return UIImage(named: "default_figure")!
 }
}

保存到系统相册

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#import <AssetsLibrary/AssetsLibrary.h>
- (void)saveVideoIntoAlbum {
 NSString *fileDirectory =
 NSFileManager *fileManager = [NSFileManager defaultManager];
 NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:fileDirectory error:nil];
 NSString *filePath = [fileDirectory stringByAppendingPathComponent:[fileArray lastObject]];
 NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
 
 NSLog(@"fileUrl = %@", fileUrl);
 ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
 
 if ([assetslibrary videoAtPathIsCompatibleWithSavedPhotosAlbum:fileUrl]) {
  [assetslibrary writeVideoAtPathToSavedPhotosAlbum:fileUrl completionBlock:^(NSURL *assetURL, NSError *error) {
   NSLog(@"%@-----%@", assetURL, error);
  }];
 } else {
  
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。