【问题标题】:Draw image with transparency绘制具有透明度的图像
【发布时间】:2011-12-04 06:42:43
【问题描述】:

我有几个 png,我将它们相互叠加以在 beginGraphicsContextWithOptions... 中形成单个图像。但是,我想绘制其中一些具有透明度的图像。请问我该怎么做?

【问题讨论】:

  • 您是想在合并图像时对图像应用 Alpha,还是只想使用图像已有的透明度?
  • 图像没有透明度。我想在绘制某些图像时选择性地应用透明度。
  • 我要做的是设置一个目标缓冲区,将每个图像展开到自己的缓冲区中,一次一个像素地循环图像数据,将像素写入目标缓冲区(根据你想要的 alpha 和已经存在的内容),然后创建一个新的上下文,并使用目标缓冲区绘制一个新的 CGImage,用它制作一个新的 UIImage,这样就可以了。我现在超级累,但我周一会在办公室,我有一些代码可以帮助你。这至少应该让你知道你可以从哪里开始。同时祝你好运!

标签: iphone ios cocoa-touch ipad


【解决方案1】:

以下是您将如何使用透明度覆盖图像:

NSString* file1 = "your images file path";

NSRect largerect = NSRectMake(10,10,300,300);

NSString* file2 = "your images file path";

NSString* file3 = "your images file path";

NSString* file4 = "your images file path";

NSImage* img1 = [[NSImage alloc] initWithContentsOfFile:file1];

NSImage* img2 = [[NSImage alloc] initWithContentsOfFile:file1];

NSImage* img3 = [[NSImage alloc] initWithContentsOfFile:file1];

NSImage* img4 = [[NSImage alloc] initWithContentsOfFile:file1];

//the fraction parameter is just how transparent you want it so 1.0 is opaque and 0.0      you will not see the image

[image1 drawInRect: largeRect
      fromRect: NSZeroRect
     operation: NSCompositeSourceOver
      fraction: 0.8];

[image2 drawInRect: largeRect
      fromRect: NSZeroRect
     operation: NSCompositeSourceOver
      fraction: 0.6];

[image1 drawInRect: smallRect1
      fromRect: NSZeroRect
     operation: NSCompositeSourceOver
      fraction: 0.40];

[image2 drawInRect: smallRect2
      fromRect: NSZeroRect
     operation: NSCompositeSourceOver
      fraction: 0.40];

【讨论】:

  • 抱歉,这是一个 iOS 问题,但您的回答确实让我再次查看文档,并且 UIImage 现在有一个带有 alpha 组件的混合模式方法
猜你喜欢
  • 2018-04-02
  • 2018-02-16
  • 2016-04-13
  • 2018-05-17
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
相关资源
最近更新 更多