完成效果:
用iTunes向app導入視頻后,不用手動刷新,編寫的工具類會實時監聽復制狀態,復制完成后會自動刷新UI.
大坑:因為文件共享是共享的Document文件夾下所有資源,所以你需要把非共享的文件,如:數據庫文件、緩存文件等,存放到除Document文件夾以外的地方,如:Preferences或Caches文件夾下,否則app會被拒!!!
1.在Info.plist添加字段:
“Application supports iTunes file sharing” 值設置為“YES”;
添加之后手機連接iTunes才能在文件共享中找到自己的應用
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];
}
}