69 lines
1.9 KiB
Objective-C
69 lines
1.9 KiB
Objective-C
//
|
|
// GiGaFileNanager.m
|
|
// GIGA
|
|
//
|
|
// Created by lianxiang on 2018/8/22.
|
|
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
|
//
|
|
|
|
#import "GiGaFileNanager.h"
|
|
|
|
@implementation GiGaFileNanager
|
|
|
|
+ (GiGaFileNanager *)shareInstance
|
|
{
|
|
static GiGaFileNanager *manager = nil;
|
|
static dispatch_once_t once;
|
|
dispatch_once(&once, ^{
|
|
manager = [[self alloc] init];
|
|
});
|
|
|
|
return manager;
|
|
}
|
|
|
|
- (BOOL)isFileExistWithFilePath:(NSString *)filePath
|
|
{
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
BOOL isDirectory = FALSE;
|
|
return [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
|
|
}
|
|
|
|
-(NSString *)getFilePathWithImageName:(NSString *)imageName{
|
|
|
|
if (imageName) {
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
|
|
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName];
|
|
return filePath;
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
-(void)removeItemAtPath:(NSString *)filePath{
|
|
NSFileManager *filManager = [NSFileManager defaultManager];
|
|
[filManager removeItemAtPath:filePath error:nil];
|
|
}
|
|
|
|
- (void)saveAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName success:(void (^)(void))success
|
|
failure:(void (^)(NSError *err))failure{
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
|
|
|
|
UIImage *image = [UIImage imageWithData:data];
|
|
NSString *filePath = [self getFilePathWithImageName:imageName]; // 保存文件的名称
|
|
if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {
|
|
//NSLog(@"保存成功");
|
|
|
|
success();
|
|
|
|
}else{
|
|
//NSLog(@"保存失败");
|
|
NSError *err;
|
|
failure(err);
|
|
} });
|
|
}
|
|
|
|
|
|
@end
|