UIView drawHierarchy creating a black image

回眸只為那壹抹淺笑 提交于 2021-01-28 00:55:55

问题


I am trying to convert my custom UIView into UIImage using core graphic, but sometimes it turns to black, my whole UIImage looks like a black image.

What I noticed is that if the height of my UIView is exceeding 4096 regardless what the width is then the produced UIImage will be black!

Note 1: my custom UIView is loaded from a nib file.

Here is my code:

func getImageWithScale(_ scale: CGFloat) -> UIImage {
        let size = self.bounds.size.applying(CGAffineTransform(scaleX: scale, y: scale))

        UIGraphicsBeginImageContextWithOptions(size, true, 1)
        self.drawHierarchy(in: CGRect(origin: CGPoint.zero, size: size), afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image!
    }

来源:https://stackoverflow.com/questions/42904135/uiview-drawhierarchy-creating-a-black-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!