美文网首页
iOS 获取本地图片,转化base64字符串

iOS 获取本地图片,转化base64字符串

作者: middo | 来源:发表于2019-01-02 16:22 被阅读0次

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

NSString *fileLastPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", filePath]];

UIImage *image = [UIImage imageWithContentsOfFile:fileLastPath];

NSData *imagedata = UIImageJPEGRepresentation(image,0.001);

NSString *imageStr = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

// 可能转译的原因,可能有空格js,解析不出来,然后祛除空格

imageStr = [self removeSpaceAndNewline:imageStr];

// 图片转成base64字符串需要先取出所有空格和换行符

- (NSString *)removeSpaceAndNewline:(NSString *)str

{

    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];

    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];

    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    return temp;

}

相关文章

网友评论

      本文标题:iOS 获取本地图片,转化base64字符串

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