Making deep copy of UIImage

前端 未结 6 1708
梦谈多话
梦谈多话 2020-12-16 15:44

My class contains an UIImage property which I want to enforce as a \'copy\' property by any external clients accessing it. But, when I try to do a copy in my custom setter,

6条回答
  •  执笔经年
    2020-12-16 15:59

    Code for Swift 5.1:

        let imageToCopy = <# your_image #>
        UIGraphicsBeginImageContext(imageToCopy.size)
        imageToCopy.draw(in: CGRect(x: 0, y: 0, width: imageToCopy.size.width, height: imageToCopy.size.height))
        let copiedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    

提交回复
热议问题