将字节数组编码为Objective-C中的JPEG图像

时间:2022-09-25 21:19:02

I have a file of raw data , its a image data. Now i need to convert the Raw data to JPEG image in Objective-C. STEPS:

我有一个原始数据文件,它是一个图像数据。现在我需要在Objective-C中将原始数据转换为JPEG图像。脚步:

1) Read the file containing the raw data into NSString. 2) Encode the string to JPEG encoder 3) Create an JPEG image

1)将包含原始数据的文件读入NSString。 2)将字符串编码为JPEG编码器3)创建JPEG图像

Can you please guide me how to achieve this?

你能指导我如何实现这个目标吗?

Is there any library available in iPhone OS to encode into JPEG.

iPhone OS中是否有可用于编码为JPEG的库。

1 个解决方案

#1


4  

This is how you create a UIImage from JPEG data:

这是您从JPEG数据创建UIImage的方法:

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfFile:…]];

And this is how you create a JPEG representation of a UIImage:

这就是你如何创建UIImage的JPEG表示:

NSData *data = UIImageJPEGRepresentation(anImage, compressionQuality);

…but only after writing this I realized you probably want to create a JPEG image from raw image sample data? In that case you’ll probably have to create a CGImage with the correct image sample format and supply the bitmap data using a provider, see CGImageCreate. (Hopefully somebody can correct me or come up with sample code.) Then you can easily create an UIImage from the CGImage and use the code above.

...但只有在写完之后我才意识到你可能想要从原始图像样本数据创建一个JPEG图像?在这种情况下,您可能必须使用正确的图像样本格式创建CGImage并使用提供程序提供位图数据,请参阅CGImageCreate。 (希望有人可以纠正我或提出示例代码。)然后,您可以轻松地从CGImage创建UIImage并使用上面的代码。

#1


4  

This is how you create a UIImage from JPEG data:

这是您从JPEG数据创建UIImage的方法:

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfFile:…]];

And this is how you create a JPEG representation of a UIImage:

这就是你如何创建UIImage的JPEG表示:

NSData *data = UIImageJPEGRepresentation(anImage, compressionQuality);

…but only after writing this I realized you probably want to create a JPEG image from raw image sample data? In that case you’ll probably have to create a CGImage with the correct image sample format and supply the bitmap data using a provider, see CGImageCreate. (Hopefully somebody can correct me or come up with sample code.) Then you can easily create an UIImage from the CGImage and use the code above.

...但只有在写完之后我才意识到你可能想要从原始图像样本数据创建一个JPEG图像?在这种情况下,您可能必须使用正确的图像样本格式创建CGImage并使用提供程序提供位图数据,请参阅CGImageCreate。 (希望有人可以纠正我或提出示例代码。)然后,您可以轻松地从CGImage创建UIImage并使用上面的代码。