Iphone 如何使用Objective C将.jpg图像转换为.bmp格式?

Iphone 如何使用Objective C将.jpg图像转换为.bmp格式?,iphone,objective-c,Iphone,Objective C,有人知道如何在iphone中使用objective-C将.jpg图像转换为.bmp格式吗? 我如何处理(或RGB颜色)IPhone设备上的每个像素的彩色图像? 是否需要转换图像类型 您将无法在iPhone上轻松获得bmp表示。在Mac上的Cocoa中,它由NSBitmapImageRep类管理,并且非常简单,如下所述 在较高级别上,您需要将.jpg转换为NSBitmapImageRep对象,然后让框架为您处理转换: a。将JPG图像转换为NSBitmapImageRep b。使用内置的NSBit

有人知道如何在iphone中使用objective-C将.jpg图像转换为.bmp格式吗? 我如何处理(或RGB颜色)IPhone设备上的每个像素的彩色图像?
是否需要转换图像类型

您将无法在iPhone上轻松获得bmp表示。在Mac上的Cocoa中,它由NSBitmapImageRep类管理,并且非常简单,如下所述

在较高级别上,您需要将.jpg转换为NSBitmapImageRep对象,然后让框架为您处理转换:

a。将JPG图像转换为NSBitmapImageRep

b。使用内置的NSBitmapImageRep方法以所需格式保存

NSBitmapImageRep *origImage = [self documentAsBitmapImageRep:[NSURL fileURLWithPath:pathToJpgImage]];
NSBitmapImageRep *bmpImage = [origImage representationUsingType:NSBMPFileType properties:nil];

- (NSBitmapImageRep*)documentAsBitmapImageRep:(NSURL*)urlOfJpg;
{

    CIImage *anImage = [CIImage imageWithContentsOfURL:urlOfJpg];
    CGRect outputExtent = [anImage extent];

    // Create a new NSBitmapImageRep.
    NSBitmapImageRep *theBitMapToBeSaved = [[NSBitmapImageRep alloc]  
                                            initWithBitmapDataPlanes:NULL pixelsWide:outputExtent.size.width  
                                            pixelsHigh:outputExtent.size.height  bitsPerSample:8 samplesPerPixel:4  
                                            hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace  
                                            bytesPerRow:0 bitsPerPixel:0];

    // Create an NSGraphicsContext that draws into the NSBitmapImageRep.  
    NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:theBitMapToBeSaved];

    // Save the previous graphics context and state, and make our bitmap context current.
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext: nsContext];
    CGPoint p = CGPointMake(0.0, 0.0);

    // Get a CIContext from the NSGraphicsContext, and use it to draw the CIImage into the NSBitmapImageRep.
    [[nsContext CIContext] drawImage:anImage atPoint:p fromRect:outputExtent];

    // Restore the previous graphics context and state.
    [NSGraphicsContext restoreGraphicsState];

    return [[theBitMapToBeSaved retain] autorelease];

}
在iPhone上,UIKit不直接支持BMP,因此您必须亲自访问并管理转换

逐像素处理要复杂得多。同样,如果对您来说这是一个硬要求,那么您应该非常熟悉设备上的核心图形功能

  • 将JPG图像加载到可以本机处理的
    UIImage
  • 然后,您可以从UIImage对象中获取
    cImageRef
  • 创建一个新的位图CG图像上下文,其属性与您已有的图像相同,并提供您自己的数据缓冲区来保存位图上下文的字节
  • 将原始图像绘制到新位图上下文中:提供的缓冲区中的字节现在是图像的像素
  • 现在需要对实际的BMP文件进行编码,这不是UIKit或CoreGraphics(据我所知)框架中存在的功能。幸运的是,这是一种故意使用的简单格式——我已经在一个小时或更短的时间内为BMP编写了快速而肮脏的编码器。以下是规范:(版本3应该可以,除非您需要支持alpha,但是来自JPEG的您可能不支持。)

  • 祝你好运。

    你坚持BMP吗?还是你想要原始的RGB数据?嗨,你的回答对我很有帮助,谢谢。我可以阅读或比较这些单词(位于图像上)与其他单词(也位于图像上)吗?如果可能的话,我是怎么做的?你是说图像中的实际渲染文本吗?如果是这样的话,那么从图像中识别文本是一个完整的研究领域(如果你指的是字节块中的单词,那就不同了)