iOS iTunes导入本地视频(文件共享)

完成效果:

用iTunes向app导入视频后,不用手动刷新,编写的工具类会实时监听复制状态,复制完成后会自动刷新UI.


大坑:因为文件共享是共享的Document文件夹下所有资源,所以你需要把非共享的文件,如:数据库文件、缓存文件等,存放到除Document文件夹以外的地方,如:PreferencesCaches文件夹下,否则app会被拒!!!


1.在Info.plist添加字段:

“Application supports iTunes file sharing” 值设置为“YES”;
添加之后手机连接iTunes才能在文件共享中找到自己的应用

iOS iTunes导入本地视频(文件共享)_第1张图片
WechatIMG1.jpeg
2.单例工具类FileWatcher的启动与注销

启动内容包括:

  • 获取app中已经用iTunes导入的视频
  • 开启对Document文件夹的监听
  • 相关属性的初始化
@interface FileWatcher ()
@property (nonatomic, strong)  dispatch_source_t source;
@property (nonatomic, strong) NSMutableArray *videoNameArr;
@property (nonatomic, assign) BOOL isConvenientFinished; //一次遍历完成标识
@property (nonatomic, assign) BOOL isFinishedCopy; //复制完成标识
@end
- (void)startManager {
    self.dataSource = [[NSMutableArray alloc] init];
    self.videoNameArr = [[NSMutableArray alloc] init];
    self.isFinishedCopy = YES;  
    self.isConvenientFinished = YES;
    [self getiTunesVideo];
    [self startMonitorFile];
}

- (void)stopManager {
    dispatch_cancel(self.source);
}
3.Document文件夹监听方法
- (void)startMonitorFile {  //监听Document文件夹的变化
    
    NSURL *directoryURL = [NSURL URLWithString:[SandBoxHelper docPath]]; //添加需要监听的目录    
    int const fd =
    open([[directoryURL path] fileSystemRepresentation], O_EVTONLY);
    if (fd < 0) {     
        NSLog(@"Unable to open the path = %@", [directoryURL path]);
        return;
    }    
    dispatch_source_t source =
    dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fd,
                           DISPATCH_VNODE_WRITE,
                           DISPATCH_TARGET_QUEUE_DEFAULT);
    dispatch_source_set_event_handler(source, ^() {
        unsigned long const type = dispatch_source_get_data(source);
        switch (type) {
            case DISPATCH_VNODE_WRITE: {
                NSLog(@"Document目录内容发生变化!!!");
                if (self.isConvenientFinished) {
                    self.isConvenientFinished = NO;
                    [self directoryDidChange];
                }
                break;
            }
            default:
                break;
        }
    });
    
    dispatch_source_set_cancel_handler(source, ^{
        close(fd);        
    });    
    self.source = source;
    dispatch_resume(self.source);
}
4.将iTunes导入的视频显示出来的核心方法
- (void)directoryDidChange {
    [self getiTunesVideo];
}

- (void)getiTunesVideo {    
    dispatch_async(fileWatcher_queue(), ^{
        //获取沙盒里所有文件
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //在这里获取应用程序Documents文件夹里的文件及文件夹列表
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDir = [documentPaths objectAtIndex:0];
        NSError *error = nil;
        NSArray *fileList = [[NSArray alloc] init];
        //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
        fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
        if (fileList.count > 0) {
            for (NSString *file in fileList) {
                //在这里添加资源的过滤条件
                if ([file hasSuffix:@".mov"] ||[file hasSuffix:@".mp4"] || [file hasSuffix:@".m4v"]) {
                    NSString *videoPath = [documentDir stringByAppendingPathComponent:file];
                    NSArray *lyricArr = [videoPath componentsSeparatedByString:@"/"];
                   //此判断的作用:避免同一资源的反复添加,使资源只添加过一次后,只要不删,就不会再重新获取路径、图片等
                    if (![self.videoNameArr containsObject:[lyricArr lastObject]]) {
                        [self.videoNameArr addObject:[lyricArr lastObject]];
                        //===============================循环判断是否复制完成==============================================
                        NSInteger lastSize = 0;
                        NSDictionary *fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:videoPath error:nil];
                        NSInteger fileSize = [[fileAttrs objectForKey:NSFileSize] intValue];
                        do {
                            lastSize = fileSize;
                            [NSThread sleepForTimeInterval:0.5];
                            self.isFinishedCopy = NO;
                            fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:videoPath error:nil];
                            fileSize = [[fileAttrs objectForKey:NSFileSize] intValue];
                            NSLog(@"%@文件正在复制", [lyricArr lastObject]);
                        } while (lastSize != fileSize);
                        self.isFinishedCopy = YES;
                        NSLog(@"%@文件复制完成", [lyricArr lastObject]);
                        VideoModel *model = [[VideoModel alloc] init];
                        model.videoPath = videoPath;
                        model.videoName = [lyricArr lastObject];
                        model.videoSize = [SandBoxHelper fileSizeForPath:videoPath];
                        model.videoImgPath = [self saveImg:[UIImage getThumbnailImage:videoPath] withVideoMid:[NSString stringWithFormat:@"%lld", model.videoSize]];
                        model.videoAsset = nil;
                        [self.dataSource addObject:model];
                        ///为防止一次同时拖入多个文档,使得数据加载不全,特做一次递归处理。
                        [self directoryDidChange];
                    }
                    [[NSNotificationCenter defaultCenter] postNotificationName:RefreshiTunesUINotification object:nil];
                }
            }
        }
        self.isConvenientFinished = YES;
    });
}
4.1.拿到导入视频的缩略图,并存到本地
+(UIImage *)getThumbnailImage:(NSString *)videoURL {    
    if (videoURL) {
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];       
        AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];        
        // 设定缩略图的方向
        // 如果不设定,可能会在视频旋转90/180/270°时,获取到的缩略图是被旋转过的,而不是正向的
        gen.appliesPreferredTrackTransform = YES;    
        // 设置图片的最大size(分辨率)
        gen.maximumSize = CGSizeMake(300, 169);       
        CMTime time = CMTimeMakeWithSeconds(5.0, 600); //一秒钟600帧,取第10帧。
        NSError *error = nil;
        CMTime actualTime; 
        CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];    
        if (error) {
            UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
            return placeHoldImg;
        }  
        UIImage *thumb = [[UIImage alloc] initWithCGImage:image];  
        CGImageRelease(image);   
        return thumb;      
    } else { 
         UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
        return placeHoldImg;
    }
}
- (NSString *)saveImg:(UIImage *)image withVideoMid:(NSString *)videoMid{  
    if (!image) {
        image = [UIImage imageNamed:@"posters_default_horizontal"];
    }
    if (!videoMid) {
        videoMid = [NSString uuid];
    }
    //png格式
    NSData *imagedata=UIImagePNGRepresentation(image); 
    NSString *savedImagePath = [[SandBoxHelper iTunesVideoImagePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", videoMid]];
    [imagedata writeToFile:savedImagePath atomically:YES];  
    return savedImagePath;    
}
5.删除文件
- (void)deleteiTunesVideo:(NSArray *)array {
    for (VideoModel *item in array) {
        [self.dataSource removeObject:item];
        [SandBoxHelper deleteFile:item.videoPath];
        [SandBoxHelper deleteFile:item.videoImgPath];
        [self.videoNameArr removeObject:item.videoName];
    }
}

以上为文件共享的开发步骤,关于如何使用封装好的工具类,请参考代码

GitHub:https://github.com/YZQ-Nine/GetLocalVideo

你可能感兴趣的:(iOS iTunes导入本地视频(文件共享))