[英]SWIFT 3 - CGImage copy always nil
having this problem and trying to fix it for hours. 有这个问题,并试图修复它几个小时。
func changeColorByTransparent(colorMasking : [CGFloat]) {
UIGraphicsBeginImageContext(self.symbolImageView.frame.size)
self.symbolImageView.layer.render(in: UIGraphicsGetCurrentContext()!)
self.symbolImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let image = self.symbolImageView.image {
print("image ok")
if let cgimage = image.cgImage {
print("cgimage ok")
if let imageRef = cgimage.copy(maskingColorComponents: colorMasking) {
print("imageRef ok")
self.symbolImageView.image = UIImage(cgImage: imageRef, scale: (self.symbolImageView.image?.scale)!, orientation: (self.symbolImageView.image?.imageOrientation)!)
}
}
}
}
The console give me : 控制台给我:
image ok
cgimage ok
It always tell me that imageRef is nil but the debug find an image for symbolImageView.image and this image is in JPG. 它总是告诉我imageRef是nil但调试找到symbolImageView.image的图像,这个图像是JPG。 The colorMasking is like colorMasking = [222,255,222,255,222,255] so no problem neither.
colorMasking就像colorMasking = [222,255,222,255,222,255]所以没有问题。
Thank you very much for your time. 非常感谢您的宝贵时间。
The source for copy(maskingColorComponents components: [CGFloat])
cannot have an alpha component. copy(maskingColorComponents components: [CGFloat])
源copy(maskingColorComponents components: [CGFloat])
不能有alpha组件。 The way you are getting an image with UIGraphicsGetImageFromCurrentImageContext
results in an alpha component. 使用
UIGraphicsGetImageFromCurrentImageContext
获取图像的方式会产生alpha分量。
Try this: 尝试这个:
func changeColorByTransparent(imgView: UIImageView, cMask: [CGFloat]) -> UIImage? {
var returnImage: UIImage?
if let capImage = imgView.image {
let sz = capImage.size
UIGraphicsBeginImageContextWithOptions(sz, true, 0.0)
capImage.draw(in: CGRect(origin: CGPoint.zero, size: sz))
let noAlphaImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let noAlphaCGRef = noAlphaImage?.cgImage
if let imgRefCopy = noAlphaCGRef?.copy(maskingColorComponents: cMask) {
returnImage = UIImage(cgImage: imgRefCopy)
}
}
return returnImage
}
and call it like this: 并称之为:
var cMask : [CGFloat] = []
cMask = [222, 255, 222, 255, 222, 255]
let newImage = changeColorByTransparent(imgView: symbolImageView, cMask: cMask)
(assuming you have a UIImageVIew
named "symbolImageView") (假设您有一个名为“symbolImageView”的
UIImageVIew
)
and, to get around an odd alpha channel issue when saving the resulting image: 并且,在保存生成的图像时绕过奇怪的alpha通道问题:
func saveImageWithAlpha(theImage: UIImage, destFile: URL) -> Void {
// odd but works... solution to image not saving with proper alpha channel
UIGraphicsBeginImageContext(theImage.size)
theImage.draw(at: CGPoint.zero)
let saveImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let img = saveImage, let data = UIImagePNGRepresentation(img) {
try? data.write(to: destFile)
}
}
I'd have look into this one a little more to figure out what's actually wrong. 我会更多地研究这个,以找出实际上是错的。
Let's start with this: break your if-let into a nested if-let. 让我们从这开始:将if-let打破为嵌套的if-let。 Just for the sake of figuring out which value is returning nil.
只是为了弄清楚哪个值返回零。
if let image = self.symbolImageView.image? {
if let cgImage = image.cgImage {
...
}
}
Set some break points, step through and let me know what you find. 设置一些断点,一步一步让我知道你发现了什么。
______________________________EDIT ONE______________________________ ______________________________EDIT ONE______________________________
So taking a quick look at the docs for copy(maskingColorComponents:) it seems like there could be a problem with the components array. 因此,快速查看复制文档 (maskingColorComponents :),似乎组件数组可能存在问题。
Components: 组件:
An array of color components that specify a color or range of colors to mask the image with.
一组颜色组件,指定用于遮罩图像的颜色或颜色范围。 The array must contain 2N values { min 1 , max 1 , ... min[N], max[N] } where N is the number of components in color space of image.
该数组必须包含2N个值{min 1 ,max 1 ,... min [N],max [N]},其中N是图像颜色空间中的分量数。 Each value in components must be a valid image sample value.
组件中的每个值必须是有效的图像样本值。 If image has integer pixel components, then each value must be in the range [0 .. 2**bitsPerComponent - 1] (where bitsPerComponent is the number of bits/component of image).
如果图像具有整数像素分量,则每个值必须在[0 .. 2 ** bitsPerComponent - 1]范围内(其中bitsPerComponent是图像的位数/分量)。 If image has floating-point pixel components, then each value may be any floating-point number which is a valid color component.
如果图像具有浮点像素分量,则每个值可以是作为有效颜色分量的任何浮点数。
Are you certain that your values are valid for your image? 您确定您的价值对您的形象有效吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.