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

2019-01-02  本文已影响0人  middo

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;

}

上一篇 下一篇

猜你喜欢

热点阅读