GiGaMaskTime/GIGA/Common/GiGaFileManager/GiGaFileNanager.m

91 lines
2.5 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);
} });
}
-(void)saveImaeToAlBum:(UIImage *)image{
if (image != nil) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
}
- (void)image: (UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功,可到相册查看" ;
}
[[UIApplication sharedApplication].keyWindow makeToast:msg duration:1.6 position:CSToastPositionCenter];
}
@end