美文网首页
44.将图片写入本地

44.将图片写入本地

作者: bytebytebyte | 来源:发表于2020-11-08 00:02 被阅读0次
    1.方法1,推荐使用
    
               NSData *data = [[NSData alloc]initWithBase64EncodedString:message.msg1 options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
                NSString *path_document = NSHomeDirectory();
                //设置一个图片的存储路径
                NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.jpg",[self getImageName]]];
                [data writeToFile:imagePath atomically:YES];//这儿是将缩略图写入了本地
    
    
    2.方法2
    
    //首先将图片转为NSData
    
    NSData *data = [[NSData alloc]initWithBase64EncodedString:message.msg1 options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    
     [data writeToFile:[self getImageFilePath] atomically:YES];
    
    
    - (NSString *)getImageFilePath  {
        NSString *catchPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
        NSString *imagefilepath = [catchPath stringByAppendingPathComponent:[self getImageName]];
        return imagefilepath;
    }
    -(NSString *)getImageName
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *imageName = [NSString stringWithFormat:@"%@%d.jpg", str,arc4random()%999999];
        return imageName;
    }
    

    相关文章

      网友评论

          本文标题:44.将图片写入本地

          本文链接:https://www.haomeiwen.com/subject/tjtqmktx.html