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

 


参考资料:  

 [简书地址](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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值