How to convert png/jpeg to .bmp in iOS

对着背影说爱祢 提交于 2019-12-12 06:28:13

问题


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);

回答1:


At a high level, you need to get the .jpg into an NSBitmapImageRep object and then let the frameworks handle the conversion for you.

Below is the link For MAC: How to convert .jpg image to .bmp format using Objective C?

There is a commandline tool which might help you from GITHUB

https://github.com/tonybeltramelli/TMXResolutionTool/

Also the best thing would be to get the image in UIImage and then convert it to bitMap.

https://github.com/PaulSolt/UIImage-Conversion

This will help you.

Regards,

Anil



来源:https://stackoverflow.com/questions/23405609/how-to-convert-png-jpeg-to-bmp-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!