#pragma mark - 将图片转为二进制 + (NSString *)image2binaryWithImage:(UIImage *)image imageName:(NSString *)imgName { //获取图片的二进制数据 NSData *imageData = UIImageJPEGRepresentation(image, 0.65); // if (UIImagePNGRepresentation(image) == nil) { // imageData = UIImageJPEGRepresentation(image, 0.65); // } else { // imageData = 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; }
时间: 2024-11-04 17:52:08