One: Call the download method in VC;
[HTXGolfVideoBufferMemory downLoadGolfVideoWithPath:littleVideoPath andURL:_url andWithDownLoadCompelition:^(NSString *path) {
// _url = path;
DSLog(@"Download complete address%@",path);
}];
Two: implemented in the download method;
typedef void (^DownLoadCompeteBlcok)(NSString * path);
@interface HTXGolfVideoBufferMemory : NSObject
+ (void)createFile:(NSString*)path WithData:(NSData*)imgData andWithDownLoadCompelition : (DownLoadCompeteBlcok) downCompelition;;
+ (void) downLoadGolfVideoWithPath:(NSString *)littleVideoPath andURL:(NSString *)url andWithDownLoadCompelition : (DownLoadCompeteBlcok) downCompelition;
//Create a cache path and download to the local
+ (void)createFile:(NSString*)path WithData:(NSData*)imgData andWithDownLoadCompelition : (DownLoadCompeteBlcok) downCompelition{ // file
path = [path stringByAppendingString:@"OVER"];
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
// write
NSFileHandle*dataHandle = [NSFileHandle fileHandleForUpdatingAtPath:path] ;
[dataHandle writeData:imgData];
if (downCompelition) {
downCompelition(path);
}
// close
[dataHandle closeFile];
}
//创建多线程下载
+ (void) downLoadGolfVideoWithPath:(NSString *)littleVideoPath andURL:(NSString *)url andWithDownLoadCompelition:(DownLoadCompeteBlcok) downCompelition{
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSData *littleVideoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
[self createFile:littleVideoPath WithData:littleVideoData andWithDownLoadCompelition:downCompelition];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
DSLog(@"视频缓存成功");
}];
}];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:operation];
}