Objective-C 中多种图像格式之间的转换
我希望能够在获得图像路径的情况下将该图像转换为另一个图像,但格式不同。我所说的格式是指 .png、.bmp .jpg .tiff 等。在伪代码中,这非常简单:
image = [ImageAPI open: imagePath]
[image save: imagePath withFormat: IMAGEFORMAT] // this is important
但我不知道如何做到这一点。在处理图像方面,我几乎不知道 NSImage 类),但这似乎没有解决我的问题(没有像这样的简单保存方法)。以下是我想要支持的格式列表:
- .png
- .tiff
- .gif
- .jpeg
- .bmp
- 可能 .pdf (如果不是很复杂)
我所说的支持是指打开其中任何一个并将打开的图像保存到任何格式中这些格式。这里有一些组合(15 ??? - 我认为),所以我可以为每个组合编写一个方法,如下所示:
[self saveTiffImageIntoGifWithPath: path] (*15 !!!)
但使用 API 肯定更好。
如果万一我还有这样的选择:
- 保留 Alpha 或不
- 调整图像大小
……我也很乐意支持它们,尽管它们是可选的。 我真的希望有一种简单的方法可以做到这一点。谢谢
PS:我知道有人对此主题有疑问,但它们都是面向 iPhone 的(我想在 Mac 上执行此操作),并且没有一个提供使用多种格式执行此操作的方法。
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
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 (if it isn't very complicated)
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 !!!)
But it is defenitely better to use APIs.
If by any chance along the way I also have options like:
- keep alpha or not
- resize images
...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: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,在彼得的帮助下我可以解决这个问题。首先,如果您正在使用图像路径,则直接使用 NSData 打开文件,如下所示:
如果您正在使用 NSImage 对象并且很难获得路径(例如使用 NSImageView),则执行此操作(图像是您拥有的 NSImage 对象):
现在您已将图像放入 NSData 对象中,将其放入 NSBitmapImageRep 中:
然后获取一个新的 NSData 对象,但格式不同:
最后,将 newData 保存到文件中:
简单如馅饼,一旦你得到它是如何运作的!
对透明度和大小控制的支持也不是那么困难:
只需在需要时调用它们即可。可可+1!
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:
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):
Now that you have your image into NSData objects, get it into a NSBitmapImageRep:
And then get a new NSData object, but with a different format:
Finally, save that newData into a file:
Simple as pie, once you get how it works!
The support for transparency and size control is not that difficult either:
Just call those when you need. +1 for Cocoa!
从 NSImage 向外探索会导致< a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/" rel="nofollow">NSBitmapImageRep,它正是您想要的。
其中一些 iPhone 问题也相关,因为适用于两个平台的解决方案(以及当今 NSBitmapImageRep 解决方案背后的实现)是使用 CGImageSource 读取图像和 CGImageDestination 将其写出来。
Exploring outward from NSImage leads you to NSBitmapImageRep, which does exactly what you want.
Some of those iPhone questions are relevant as well, because the solution that works on both platforms (and the implementation behind the NSBitmapImageRep solution nowadays) is to use CGImageSource to read in the image and CGImageDestination to write it out.