CGImage.masking(_ :)不应用遮罩

时间:2019-05-03 23:44:02

标签: swift mask cgimage

我正在尝试使用从原始数据创建的CGImage作为另一幅图像的图像蒙版。根据{{​​3}},要用作图像遮罩的图像必须位于GreyScale色彩空间中,并且不具有alpha分量。满足两个要求。用作图像遮罩的图像上半部分包含黑色像素,下半部分包含白色像素。因此,将其应用于由红色像素组成的图像后,必然会使红色图像的一半消失(下面的代码可以直接粘贴到运动场中)。但正如您所见,它没有!

任何帮助都是非常感谢的,因为我已经尝试了许多不同的方法,例如使用其他图像蒙版初始化程序(而不是使用图像)。最初,我尝试过使用ARGB格式的蒙版,但是那也不起作用。 谢谢!

import UIKit

struct ARGBPixel {
    var alpha: UInt8
    var red: UInt8
    var green: UInt8
    var blue: UInt8
}

struct GreyScalePixel {
    var value: UInt8
}

let greyScaleCS = CGColorSpaceCreateDeviceGray()
greyScaleCS.baseColorSpace
greyScaleCS.numberOfComponents

//create solid red image
let width = 100
let height = 100
let size = width * height

let solidRedPixel = ARGBPixel(alpha: 255, red: 255, green: 0, blue: 0)
let solidRedPixels = [ARGBPixel](repeating: solidRedPixel, count: size)
let solidRedPixelsPtr: UnsafePointer<UInt8> = UnsafeRawPointer(solidRedPixels).assumingMemoryBound(to: UInt8.self)
let data: CFData = CFDataCreate(nil, solidRedPixelsPtr, size *  MemoryLayout<ARGBPixel>.stride)!
let dataProvider: CGDataProvider = CGDataProvider(data: data)!
let solidRed = CGImage(width: width,
                           height: width,
                           bitsPerComponent: 8,
                           bitsPerPixel: 32,
                           bytesPerRow: width * 4,
                           space: CGColorSpaceCreateDeviceRGB(),
                           bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.first.rawValue),
                           provider: dataProvider,
                           decode: nil,
                           shouldInterpolate: true,
                           intent: CGColorRenderingIntent.defaultIntent)!
let imgRed = UIImage(cgImage: solidRed)


//create mask consisiting of 5000 black and 5000 white greyscal pixels
let blackPixel = GreyScalePixel(value: 0)
let whitePixel = GreyScalePixel(value: 255)
let blackPixels = [GreyScalePixel](repeating: blackPixel, count: size/2)
let whitePixels = [GreyScalePixel](repeating: whitePixel, count: size/2)
let maskPixels = blackPixels + whitePixels
let maskPixelPtr: UnsafePointer<UInt8> = UnsafeRawPointer(maskPixels).assumingMemoryBound(to: UInt8.self)
let maskData: CFData = CFDataCreate(nil, maskPixelPtr, size *  MemoryLayout<GreyScalePixel>.stride)!
let maskDataProvider: CGDataProvider = CGDataProvider(data: maskData)!
let maskImage = CGImage(width: width,
                   height: height,
                   bitsPerComponent: 8,
                   bitsPerPixel: 8,
                   bytesPerRow: width,
                   space: CGColorSpaceCreateDeviceGray(),
                   bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue),
                   provider: maskDataProvider,
                   decode: nil,
                   shouldInterpolate: true,
                   intent:  CGColorRenderingIntent.defaultIntent)!

let maskAsImg = UIImage(cgImage: maskImage)

//apply mask
let maskedImage = solidRed.masking(maskImage)!
let result = UIImage(cgImage: maskedImage)
// result is still all red, how can I make one half transparent???```

0 个答案:

没有答案