admin管理员组文章数量:1516870
文件缓存
在我们平时的项目中往往很多时候需要做一些数据缓存,如聊天中的图片、语音、视频、文件等;因此写了篇文章,如有不妥之处,还望大家能不吝赐教,谢谢
由于功能并不复杂,就不多说了,直接上代码
KFileCache.h文件
//
// KFileCache.h
// KXiniuCloud
//
// Created by RPK on 2018/8/14.
// Copyright © 2018年 EIMS. All rights reserved.
//#import <Foundation/Foundation.h>typedef void(^SaveComplete)(NSString *filePath, NSURL *url, NSError *error);
typedef void(^WhetherFileExists)(NSString *filePath, BOOL exist);@interface KFileCache : NSObject+ (instancetype)shareInstance;/**文件缓存@param strURL 文件url@param type 类型@param saveComplete 保存完成*/
-(void)fileUrl:(NSString *)strURL type:(NSString *)type saveComplete:(SaveComplete)saveComplete;/**根据文件url获取是否存在缓存@param url 文件url@param fileExists 回调*/
-(void)filePath:(NSString *)url fileExists:(WhetherFileExists)fileExists;/**文件下载@param url 文件url@param success 成功回调@param failure 失败回调*/
- (void)downloadFileWithURL:(NSString *)urlsuccess:(void (^)(NSString *requestUrl, NSString *filePath))successfailure:(void (^)(NSString *requestUrl, NSError *error))failure;@end
//
// KFileCache.m
// KXiniuCloud
//
// Created by RPK on 2018/8/14.
// Copyright © 2018年 EIMS. All rights reserved.
//#import "KFileCache.h"#import <AFNetworking.h>static KFileCache *fileCache = nil;#define fileCachePath [kDocDir stringByAppendingPathComponent:@"FileCache"]@implementation KFileCache+ (instancetype)shareInstance {static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{fileCache = [[super allocWithZone:NULL] init];});return fileCache;
}+ (instancetype)allocWithZone:(struct _NSZone *)zone {return [KFileCache shareInstance];
}/**文件缓存@param strURL 文件url@param type 类型@param saveComplete 保存完成*/
-(void)fileUrl:(NSString *)strURL type:(NSString *)type saveComplete:(SaveComplete)saveComplete
{dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//开个子线程处理图片保存[self filePath:strURL fileExists:^(NSString *filePath, BOOL exist) {if (!exist) {// 没有保存需要下载文件[self downloadFileWithURL:strURLsuccess:^(NSString *requestUrl, NSString *filePath) {if (saveComplete) {saveComplete(filePath, [NSURL URLWithString:requestUrl], nil);}}failure:^(NSString *requestUrl, NSError *error) {if (saveComplete) {saveComplete(nil, [NSURL URLWithString:requestUrl], error);}}];}else {saveComplete(filePath, [NSURL URLWithString:strURL], nil);}}];});
}/**根据文件url获取是否存在缓存@param url 文件url@param fileExists 回调*/
-(void)filePath:(NSString *)url fileExists:(WhetherFileExists)fileExists
{NSString *fPath = nil;if (![url isKindOfClass:[NSNull class]]){if ([url hasPrefix:@"http"] || [url hasPrefix:@"https"]){NSArray *arr = [url componentsSeparatedByString:@"/"];BOOL directory = YES;BOOL fDirectory = YES;NSString *fileName = [arr lastObject];NSFileManager *fileManager = [NSFileManager defaultManager];if (![fileManager fileExistsAtPath:fileCachePath isDirectory:&directory]){[fileManager createDirectoryAtPath:fileCachePath withIntermediateDirectories:NO attributes:nil error:nil];}fPath = [fileCachePath stringByAppendingPathComponent:fileName];if ([fileManager fileExistsAtPath:fPath isDirectory:&fDirectory]) {if (fileExists) {// 已经保存或该文件fileExists(fPath, YES);}}else {if (fileExists) {// 没有保存过该文件fileExists(fPath, NO);}}}}
}/**文件下载@param url 文件url@param success 成功回调@param failure 失败回调*/
- (void)downloadFileWithURL:(NSString *)urlsuccess:(void (^)(NSString *requestUrl, NSString *filePath))successfailure:(void (^)(NSString *requestUrl, NSError *error))failure
{/* 创建网络下载对象 */AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];/* 下载地址 */NSURL *kUrl = [NSURL URLWithString:url];NSURLRequest *request = [NSURLRequest requestWithURL:kUrl];/* 下载路径 */
// NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];NSString *kFilePath = [fileCachePath stringByAppendingPathComponent:url.lastPathComponent];/* 开始请求下载 */NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {NSLog(@"下载进度:%.0f%", downloadProgress.fractionCompleted * 100);} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {/* 设定下载到的位置 */return [NSURL fileURLWithPath:kFilePath];} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {if (!error) {success(url, kFilePath);}else {failure(url, error);}NSLog(@"下载完成");}];[downloadTask resume];}@end
本文标签: 文件缓存
版权声明:本文标题:文件缓存 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1686808084a38431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论