UIView转UIImage

- (UIImage *)snapshotImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

效果:有​​黑底​

iOS UIView转UIImage与CALayer转UIImage的差异_代码生成


CALayer转UIImage

- (UIImage *)snapshotImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
[self renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

效果:没黑底

iOS UIView转UIImage与CALayer转UIImage的差异_代码生成_02


一行代码生成渐变色

​https://github.com/xjh093/JHDraw​