2017-01-10 83 views
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) 

...

現在,我得到了編輯的圖像成功。

然後我添加了刪除白色背景的代碼,但我總是無。

有沒有辦法解決這個問題?

回答