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

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容