2
我的目标是从png图像中删除白色背景。为什么cgimage.copy总是在快速返回零3?
首先,我用一个PNG有白色背景去除。
这是我的代码。
...
let imageRef = self.imageView.image?.cgImage?.copy(maskingColorComponents:[222,255,222,255,222,255,222,255])
self.imageView.image = UIImage(cgImage: imageRef, scale: self.imageView.image?.scale, orientation: self.imageView.image.imageOrientation)
...
它运作良好。
然后我编辑图像并使用波纹管代码接收编辑后的图像。
...
UIGraphicsBeginImageContext(self.imageView.image.frame.size)
self.imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imageRef = self.imageView.image?.cgImage?.copy(maskingColorComponents:[222,255,222,255,222,255,222,255])
// imageRef is always nil ???
self.imageView.image = UIImage(cgImage: imageRef, scale: self.imageView.image?.scale, orientation: self.imageView.image.imageOrientation)
...
现在,我得到了编辑的图像成功。
然后我添加了删除白色背景的代码,但我总是无。
有没有办法解决这个问题?