base64编码转化为图片出现了问题

command是phonegap传来的参数列表 ,url可以输出图片的base64编码

NSString *url = [command.arguments objectAtIndex:0];

// 去除base64前缀 取前20做对比 并作为文件名存储
//NSLog(@"%@",url);
NSString *imgName = [url substringWithRange:NSMakeRange(28, 42)];
NSLog(@"%@",imgName);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSLog(@"%@",documentsPath);
NSString *imgPath = [NSString stringWithFormat:@"%@/img",documentsPath];
if(![[NSFileManager defaultManager]fileExistsAtPath:imgPath]){
    [[NSFileManager defaultManager]createDirectoryAtPath:imgPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSData *imgData =[[NSData alloc]initWithBase64EncodedString:url options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *imgNameA = [imgPath stringByAppendingFormat:@"%@/.png",imgName];
if (![[NSFileManager defaultManager]fileExistsAtPath:imgNameA]) {
    UIImage *image = [UIImage imageWithData:imgData];
    NSLog(@"%@",image);
    [UIImagePNGRepresentation(image) writeToFile:imgNameA atomically:YES];
    //[UIImageJPEGRepresentation(image, 1.0) writeToFile:imgNameA atomically:YES];
    
}

图片描述
图片描述

阅读 9.4k
1 个回答

你难道没看到 imgData是 nil

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题