视频下载

2023-05-05  本文已影响0人  小木虫666
+ (void)downloadVideoWithUrl:(NSString *)videoUrl
                    progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock
           completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
{
    NSParameterAssert(videoUrl);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSURL *url = [NSURL URLWithString:videoUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
      
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        return [self videoUrlWithPath:response.suggestedFilename];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        if (completionHandler) {
            completionHandler(response,filePath,error);
        }
    }];
    
    [download resume];
}

+ (NSURL *)videoUrlWithPath:(NSString *)component
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:component];
    return [NSURL fileURLWithPath:path];
}
上一篇 下一篇

猜你喜欢

热点阅读