首页 > 代码库 > 将图片转为二进制字符串

将图片转为二进制字符串

#pragma mark - 将图片转为二进制+ (NSString *)image2binaryWithImage:(UIImage *)image imageName:(NSString *)imgName{    //获取图片的二进制数据    NSData *imageData = http://www.mamicode.com/UIImageJPEGRepresentation(image, 0.65);//    if (UIImagePNGRepresentation(image) == nil) {//        imageData = http://www.mamicode.com/UIImageJPEGRepresentation(image, 0.65);>//    } else {//        imageData = http://www.mamicode.com/UIImagePNGRepresentation(image);>//    }     //把二进制数据转为Base64编码的字符串    NSString *dataStr = [Base64Tools encodeBase64Data:imageData];        //获取沙盒目录、将图片写入到本地    if (![imgName isEqualToString:@""]) {        NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imgName];        [imageData writeToFile:filePath atomically:YES];    }    return dataStr;}
//第三方工具方法
+ (NSString*)encodeBase64Data:(NSData*)data { data = [GTMBase64 encodeData:data]; NSString *base64String = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding] ; return base64String;}

 

将图片转为二进制字符串