在Objective-C中的多个图像格式之间进行转换 [英] Converting between multiple image formats in Objective-C

查看:116
本文介绍了在Objective-C中的多个图像格式之间进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够获得图片的路径,将该图片转换为另一个图片,但格式不同。格式我的意思是.png,.bmp .jpg,.tiff等。在伪代码中,这很容易:

I want to be able to, being given a path to an image, convert that image into another image, but with a different format. By format I mean .png, .bmp .jpg .tiff, etc. In pseudocode, this is really easy:

image = [ImageAPI open: imagePath]
[image save: imagePath withFormat: IMAGEFORMAT] // this is important

我不知道如何做到这一点,虽然。在处理图像时,我几乎不知道NSImage类),这似乎没有我的问题的答案(没有像这样的简单的保存方法)。以下是我想支持的格式列表:

I have no idea how to do this, though. I barely know more than the NSImage class when it comes to handling images), and that does not seem to have the answer to my problem (no simple save methods like this). Here is the list of formats I'd like to support:


  • .png

  • .tiff

  • .gif

  • .jpeg

  • .bmp

  • PROBABLY .pdf(如果不是很复杂)

  • .png
  • .tiff
  • .gif
  • .jpeg
  • .bmp
  • PROBABLY .pdf (if it isn't very complicated)

通过支持我的意思是打开任何这些并将打开的图像保存到任何的这些格式。这里有一些组合(15 ??? - 我想),所以我可以为每个组合写一个方法,如:

By support I mean opening any of those and saving the opened image into any of those formats. There are some combinations here (15 ??? - I think), so I could write a method for each combination like so:

[self saveTiffImageIntoGifWithPath: path] (*15 !!!)

API。

如果有任何机会,我也有类似的选项:

If by any chance along the way I also have options like:


  • 保持alpha效果

  • 调整图片大小

支持他们,虽然他们是可选的。
我真的希望有一个简单的方法来做到这一点。 THANKS

...I'd be happy to support them as well, although they're optional. I really hope there's a simple way to do this. THANKS

PS:我知道有关于这个主题的问题,但他们都是面向iPhone的(我想在mac上这样做),没有人提供

PS: I know there have been questions about this topic, but they were all iPhone oriented (I want to do this on the mac) and none of them provided a way to do this with multiple formats.

推荐答案

好的,所以与Peter的帮助我可以想出这一点。首先,如果你使用图像路径,然后用NSData直接打开这样的文件:

Allright, so with Peter's help I could figure this out. First, if you're working with image paths, then open directly the file with NSData like so:

NSData* imgData = [NSData dataWithContentsOfFile: filePath];

如果你使用NSImage对象,并且你很难有路径一个NSImageView)然后这样做(图像是你有NSImage对象):

If you're working with NSImage objects and is difficult for you to have the path (for example with an NSImageView) then do this (image is the NSImage object you have):

NSData* imgData = [image TIFFRepresentation];

现在你有你的图像成NSData对象,得到它NSBitmapImageRep:

Now that you have your image into NSData objects, get it into a NSBitmapImageRep:

NSBitmapImageRep* bitmap = [NSBitmapImageRep imageRepWithData: imgData];

然后获取一个新的NSData对象,但格式不同:

And then get a new NSData object, but with a different format:

NSData* newData = [bitmap representationUsingType: NSPNGFileType properties: nil];
// I used NSPNGFileType as an example, but all the
// types I wanted to convert between (except PDF) are supported

最后,将newData保存到一个文件中:

Finally, save that newData into a file:

[newData writeToFile: newPath atomically: YES]

很简单,一旦你得到它的工作原理!

Simple as pie, once you get how it works!

对透明度和大小控制的支持并不困难:

The support for transparency and size control is not that difficult either:


  • NSImage类提供了设置图片大小的支持(-setSize:)

  • NSImageRep(NSBitmapImageRep的超类)具有-setAlpha:方法

只需在需要时拨打电话。 +1为Cocoa!

Just call those when you need. +1 for Cocoa!

这篇关于在Objective-C中的多个图像格式之间进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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