0

Here is my original code.

In this case, displays my image in imageview.

let cgImge = self.test.image?.cgImage?.copy()
self.test.image = UIImage(cgImage: cgImge!, scale: (self.test.image?.scale)!, orientation: (self.test.image?.imageOrientation)!)

Here is my code during the process a project.

...
UIGraphicsBeginImageContext((self.test.image?.size)!)
self.test.layer.render(in: UIGraphicsGetCurrentContext()!)
self.test.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let cgImge = self.test.image?.cgImage?.copy()
self.test.image = UIImage(cgImage: cgImge!, scale: (self.test.image?.scale)!, orientation: (self.test.image?.imageOrientation)!)

...

But I can't display my image. What can I do to fix this issue?

Eric Chan
  • 1,192
  • 4
  • 16
  • 30

1 Answers1

0

Here is my fixed code.

UIGraphicsBeginImageContextWithOptions(self.test.bounds.size, self.test.isOpaque, 0.0)
//UIGraphicsBeginImageContext((self.test.image?.size)!)
self.test.layer.render(in: UIGraphicsGetCurrentContext()!)
//self.test.image?.draw(at: self.test.frame)
let temp: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

UIGraphicsBeginImageContextWithOptions(self.test.bounds.size, self.test.isOpaque, 1.0)
temp.draw(in: self.test.bounds)
let custom: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

let cgImge = custom.cgImage?.copy(maskingColorComponents: [222,255,222,255,222,255])
self.test.image = UIImage(cgImage: cgImge!, scale: (self.test.image?.scale)!, orientation: (self.test.image?.imageOrientation)!)

I got a reference link

iOS : Save image with custom resolution

Community
  • 1
  • 1
Eric Chan
  • 1,192
  • 4
  • 16
  • 30