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

获取缩略图

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")!
    }
}

保存到系统相册

#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 {
        
    }
}
posted @ 2017-06-12 09:09  Lawerence  阅读(620)  评论(0编辑  收藏  举报