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;
}
网友评论