为什么CGBlendMode.color在模拟器中正常工作但在设备上失败?

时间:2017-01-18 09:47:31

标签: ios swift core-graphics cgcontext cashapelayer

我使用CAShapeLayer' s render(in:)将混合模式设置为CGBlendMode.color,以便将绘制到当前CGContext的图像着色。它在模拟器中完美运行,但不在设备上。

Simulated iPhone 7 Plus Actual iPhone 7 Plus

左侧的屏幕截图是模拟的iPhone 7 Plus,右侧是运行iOS 10.2的实际设备。首先绘制黑色方块,然后绘制红色圆圈,然后将CGBlendMode设置为color并绘制蓝色圆圈。

CGBlendMode.color的{​​{3}}说:

  

使用色调和背景的背景亮度值   源图像的饱和度值。

因此,蓝色圆圈与黑色背景重叠,应使用零亮度并绘制黑色。

为什么这在模拟器中正常工作,而在真实设备上却没有?更重要的是,我怎样才能让这种混合物在设备上运行?

要重现,请使用"单一视图应用程序"在Xcode 8.2.1中创建一个新项目。模板并将ViewController.swift替换为:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let blendModeTestView = BlendModeTest(frame: CGRect(x: 100, y: 100, width: 250, height: 250))
        view.addSubview(blendModeTestView)
    }
}

class BlendModeTest: UIView {
    override func draw(_ rect: CGRect) {

        let context = UIGraphicsGetCurrentContext()!

        // black background
        let backgroundPath = UIBezierPath(rect: rect)
        let backgroundShapeLayer = CAShapeLayer()
        backgroundShapeLayer.path = backgroundPath.cgPath
        backgroundShapeLayer.fillColor = UIColor.black.cgColor
        backgroundShapeLayer.render(in: context)

        // red circle
        let circlePath = UIBezierPath(arcCenter: CGPoint(x: rect.size.width/2, y: rect.size.height/2),
                                      radius: rect.size.width/4,
                                      startAngle: CGFloat(0),
                                      endAngle:CGFloat(M_PI * 2), clockwise: true)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.cgPath
        shapeLayer.fillColor = UIColor.red.cgColor
        shapeLayer.render(in: context)

        // blue circle (with .color blend mode)
        let colorFillPath = UIBezierPath(arcCenter: CGPoint.zero,
                                         radius: rect.size.width * 0.8,
                                         startAngle: CGFloat(0),
                                         endAngle:CGFloat(M_PI * 2),
                                         clockwise: true)
        let colorFillShapeLayer = CAShapeLayer()
        colorFillShapeLayer.path = colorFillPath.cgPath
        colorFillShapeLayer.fillColor = UIColor.blue.cgColor
        context.setBlendMode(.color)
        colorFillShapeLayer.render(in: context)
    }
}

0 个答案:

没有答案