赞
踩
用iTunes向app导入视频后,不用手动刷新,编写的工具类会实时监听复制状态,复制完成后会自动刷新UI.
“Application supports iTunes file sharing” 值设置为“YES”;
添加之后手机连接iTunes才能在文件共享中找到自己的应用
启动内容包括:
* 获取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);
}
- (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);
}
- (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;
});
}
+(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;
}
- (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];
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。