Objective-C 中多种图像格式之间的转换

发布于 2024-11-18 14:57:49 字数 816 浏览 10 评论 0原文

我希望能够在获得图像路径的情况下将该图像转换为另一个图像,但格式不同。我所说的格式是指 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-11-25 14:57:49

好吧,在彼得的帮助下我可以解决这个问题。首先,如果您正在使用图像路径,则直接使用 NSData 打开文件,如下所示:

NSData* imgData = [NSData dataWithContentsOfFile: filePath];

如果您正在使用 NSImage 对象并且很难获得路径(例如使用 NSImageView),则执行此操作(图像是您拥有的 NSImage 对象):

NSData* imgData = [image TIFFRepresentation];

现在您已将图像放入 NSData 对象中,将其放入 NSBitmapImageRep 中:

NSBitmapImageRep* bitmap = [NSBitmapImageRep imageRepWithData: imgData];

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

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 保存到文件中:

[newData writeToFile: newPath atomically: YES]

简单如馅饼,一旦你得到它是如何运作的!


对透明度和大小控制的支持也不是那么困难:

  • NSImage 类提供对设置图像大小的支持(-setSize:)
  • NSImageRep(NSBitmapImageRep 的超类)具有 -setAlpha: 方法,

只需在需要时调用它们即可。可可+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:

NSData* imgData = [NSData dataWithContentsOfFile: filePath];

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

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

NSBitmapImageRep* bitmap = [NSBitmapImageRep imageRepWithData: imgData];

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

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:

  • The NSImage class provides support for setting image's size (-setSize:)
  • The NSImageRep (superclass of NSBitmapImageRep) has the -setAlpha: method

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

落叶缤纷 2024-11-25 14:57:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文