如何在iOS中将png / jpeg转换为.bmp [英] How to convert png/jpeg to .bmp in iOS
问题描述
如何将png / jpeg转换为.bmp图像?我尝试了以下解决方案,但没有运气:
How can I convert png/jpeg to a .bmp image? I have tried the following solution but no luck:
CGImageRef sourceRef = source.CGImage;
size_t width = CGImageGetWidth(sourceRef);
size_t height = CGImageGetHeight(sourceRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
size_t bitsPerComponent = 8;
size_t bytesPerPixel = 4;
size_t bytesPerRow = (width * bitsPerComponent * bytesPerPixel + 7) / 8;
size_t dataSize = bytesPerRow * height;
unsigned char *data = malloc(dataSize);
memset(data, 0, dataSize);
CGContextRef context = CGBitmapContextCreate(data, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceRef);
CGColorSpaceRelease(colorSpace);
unsigned char *bitmapData = (unsigned char *)CGBitmapContextGetData(context);
推荐答案
在高级别,您需要获得。 jpg到一个NSBitmapImageRep对象,然后让框架为你处理转换。
At a high level, you need to get the .jpg into an NSBitmapImageRep object and then let the frameworks handle the conversion for you.
以下是MAC的链接:
如何使用Objective C将.jpg图像转换为.bmp格式?
Below is the link For MAC: How to convert .jpg image to .bmp format using Objective C?
有一个命令行工具可以帮助你从GITHUB
There is a commandline tool which might help you from GITHUB
https://github.com/tonybeltramelli/TMXResolutionTool/
也是最好的事情是在UIImage中获取图像然后将其转换为bitMap。
Also the best thing would be to get the image in UIImage and then convert it to bitMap.
https://github.com/PaulSolt/UIImage-Conversion
这对你有帮助。
问候,
Anil
这篇关于如何在iOS中将png / jpeg转换为.bmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!