简体   繁体   English

图像遮罩在iOS10 beta3上失败

[英]Image masking fails on iOS10 beta3

For some time now we use the following code to mask a grayscale image without transparency to a coloured image. 现在有一段时间,我们使用以下代码掩盖灰度图像,而对彩色图像不透明。

This always worked fine until Apple released iOS 10 beta 3. Suddenly the mask is not applied anymore resulting in just a square box being returned with te given color. 在Apple发布iOS 10 beta 3之前,此方法始终可以正常工作。突然之间不再使用该蒙版,导致仅返回带有给定颜色的方盒。

The logic behind this can be found at https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html 可以在以下网址找到其背后的逻辑: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html

under the header Masking an Image with an Image Mask 在标题下用图像蒙版蒙版图像

The logic of this code: * Take an grayscale image without alpha * Create an solid image with the given color * Create a mask from the given image * Mask the solid image with the created mask * Output is a masked image with also respect for colors in between (gray might be light red ie). 此代码的逻辑:*拍摄不含alpha的灰度图像*使用给定的颜色创建纯色图像*从给定的图像创建蒙版*使用创建的蒙版对实心图像进行蒙版*输出是同时考虑到颜色的蒙版图像之间(灰色可能是浅红色)。

Has anyone an idea how to fix this function? 有谁知道如何解决此功能? If you have XCode 8 beta 3 you can run this code and on a simulator lower than iOS 10 this will work correct and on iOS 10 it will just create a square box 如果您拥有XCode 8 beta 3,则可以运行此代码,并且在低于iOS 10的模拟器上可以正常运行,而在iOS 10上,它将仅创建一个方形框

Example image: 示例图片: 范例图片

    public static func image(maskedWith color: UIColor, imageNamed imageName: String) -> UIImage? {

    guard let image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal) else {
        return nil
    }

    guard image.size != CGSize.zero else {
        return nil
    }

    guard
        let maskRef = image.cgImage,
        let colorImage = self.image(with: color, size: image.size),
        let cgColorImage = colorImage.cgImage,
        let dataProvider = maskRef.dataProvider
        else {
        return nil
    }

    guard
        let mask = CGImage(maskWidth: maskRef.width, height: maskRef.height, bitsPerComponent: maskRef.bitsPerComponent, bitsPerPixel: maskRef.bitsPerPixel, bytesPerRow: maskRef.bytesPerRow, provider: dataProvider, decode: nil, shouldInterpolate: true),
        let masked = cgColorImage.masking(mask)
        else {
        return nil
    }

    let result = UIImage(cgImage: masked, scale: UIScreen.main().scale, orientation: image.imageOrientation)

    return result
}

public static func image(with color: UIColor, size: CGSize) -> UIImage? {

    guard size != CGSize.zero else {
        return nil
    }

    let rect = CGRect(origin: CGPoint.zero, size: size)

    UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main().scale)

    defer {
        UIGraphicsEndImageContext()
    }

    guard let context = UIGraphicsGetCurrentContext() else {
        return nil
    }

    context.setFillColor(color.cgColor)
    context.fill(rect)

    let image = UIGraphicsGetImageFromCurrentImageContext()

    return image;
}

此问题已在iOS10 beta 4中解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM