如何使用Objective C将.jpg图像转换为.bmp格式? [英] How to convert .jpg image to .bmp format using Objective C?

查看:140
本文介绍了如何使用Objective C将.jpg图像转换为.bmp格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Anyone knows how to convert .jpg image to .bmp format in iphone using objective-C? And how i process(or RGB color) the each pixel of IPhone Device caputured image? Is there need to conversion of image type?

推荐答案

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

You won't be able to easily get to a bmp representation on the iPhone. In Cocoa on the Mac, it is managed by the NSBitmapImageRep class and is pretty straight forward as outlined below.

在高级别,你需要将.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:

a。将JPG图像转换为NSBitmapImageRep

a. Convert the JPG image to an NSBitmapImageRep

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

b. Use built in NSBitmapImageRep methods to save in desired formats.

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,所以你必须下载到 Quartz /核心图形并自行管理转换。

On the iPhone, BMP is not directly supported by UIKit, so you would have to drop down into Quartz/Core Graphics and manage the transformation yourself.

逐像素处理涉及更多。同样,如果这对您来说很难,您应该非常熟悉设备上的核心图形功能。

Pixel by pixel processing is much more involved. Again, you should get intimately familiar with the core graphics capabilities on the device if this is a hard requirement for you.

这篇关于如何使用Objective C将.jpg图像转换为.bmp格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆