0

我已经使用以下内容来保存 base64 图像数据。

NSData *data=[NSData dataFromBase64String:base64];
UIImage *image= [UIImage imageWithData:data];

它始终保存为 png 类型。

如何设置除 png(jpeg 等)以外的图像类型?

4

2 回答 2

2

获得后,UIImage您必须将其转换回不同格式的数据,然后您才能获得pngjpg 或 jpeg UIImage

NSData *data1 = UIImageJPEGRepresentation(image, 1.0); // 1.0 is for original you can compress it by passing the float b/w 0-1
NSData *data2 = UIImagePNGRepresentation(image);

UIImage *jpgImage = [UIImage imageWithData:data1];
UIImage *pngImage = [UIImage imageWithData:data2];

或者您也可以data在文档目录中使用图像扩展名进行编写。

[data writeToFile:[directoryPath stringByAppendingPathComponent:@"image.jpeg"] atomically:YES];
// It will save this image as jpeg. But would not work for all formats.

编辑: 请参阅此链接How to convert .jpg image to .bmp format using Objective C? .

于 2013-08-07T04:23:34.783 回答
0

你试过UIImageJPEGRepresentation吗?

于 2013-08-07T04:04:48.767 回答