IM模块-缓存图片视频简单工具类

来源:互联网 发布:ubuntu 丢失windows 编辑:程序博客网 时间:2024/06/17 01:50

 


参考资料:  

 [简书地址](http://www.jianshu.com/users/23d0ae412e19/latest_articles)



#import "CacheHelper.h"@implementation CacheHelper+ (instancetype)sharedInstance{    static id _sharedInstance = nil;    static dispatch_once_t oncePredicate;    dispatch_once(&oncePredicate, ^{        _sharedInstance = [[self alloc] init];    });    return _sharedInstance;}- (void)saveMediaTypeWithMediaType:(MessageBodyType)messageBodyType FileData:(NSData *)data  completion:(void (^)(NSString *mediaPath))completion{    NSString *mediaPath = nil;       switch (messageBodyType) {        case MessageBodyTypePhoto:{ // 照片                    mediaPath = [self savePathFormediaType:MessageBodyTypePhoto];            break;        }        case MessageBodyTypeVideo:{//视频                        mediaPath = [self savePathFormediaType:MessageBodyTypeVideo];            break;        }        case MessageBodyTypeVoice:{//录音                    mediaPath = [self savePathFormediaType:MessageBodyTypeVoice];            break;        }        default:            break;    }         // 这个路径只能是文件,才能输入。 如果是文件夹,将不会被创建。     [data writeToFile:mediaPath atomically:YES];         completion ? completion(mediaPath) : nil;}/** *  Description: 保存数据 * *  @param mediaType <#mediaType description#> * *  @return <#return value description#> */- (NSString *)savePathFormediaType:(MessageBodyType)mediaType{        NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *path = pathArray.firstObject;        NSDate *date = [[NSDate alloc]init];    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];    formatter.dateFormat = @"YYYY-MM-dd";    NSString *timeStr = [formatter stringFromDate:date];        NSString *typeStr = nil;        switch (mediaType) {        case MessageBodyTypePhoto:{                        typeStr = [NSString stringWithFormat:@"%d.png",arc4random_uniform(1000)];            break;        }        case MessageBodyTypeVideo:{                    typeStr = @"video.mov";            break;        }        case MessageBodyTypeVoice:{                    typeStr = @"voice.m4a";            break;        }        default:            break;    }        path = [[path stringByAppendingPathComponent:timeStr] stringByAppendingString:typeStr];    return path;}@end


0 0
原创粉丝点击