我有一个带有黑色文本的视图,我想围绕它创建一个白色的“发光”。我想我可以通过截取屏幕截图、反转颜色(只有黑色和白色)、将黑色遮盖为透明,然后在每个方向上“抖动”生成的图像来做到这一点。当我尝试用 CGImageCreateWithMaskingColors 掩盖黑色时,我得到一个空的 CGImageRef。到目前为止,这就是我所拥有的。
//First get a screenshot into a CI image so I can apply a CI Filter
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
CIImage* ciImage = [[CIImage alloc] initWithCGImage:[UIGraphicsGetImageFromCurrentImageContext() CGImage]];
UIGraphicsEndImageContext();
//Now apply the CIColorInvert filter
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert" keysAndValues:kCIInputImageKey, ciImage, nil];
ciImage = [filter valueForKey:kCIOutputImageKey];
//Now I need to get a CG image from the CI image.
CIContext* ciContext = [CIContext contextWithOptions:nil];
CGImageRef ref = [ciContext createCGImage:ciImage fromRect:[ciImage extent]];
//Now I try to mask black
const float maskingColor[6] = {0,0,0,0,0,0};
ref = CGImageCreateWithMaskingColors(ref, maskingColor); //ref is (null)
我知道 alpha 通道会破坏作品,但我真的不认为我在这里有任何 alpha 通道。只是为了检查我做CGImageGetColorSpace(ref)
了并得到了kCGColorSpaceDeviceRGB
,没有 alpha 通道。
有人可以告诉我哪里出错了吗?可选地,帮助我理解 UIImage、CIImage 和 CGImage 之间差异的快速评论会很棒。