iOS UIView转UIImage与CALayer转UIImage的差异

UIView转UIImage

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

效果:有黑底
在这里插入图片描述


CALayer转UIImage

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

效果:没黑底
在这里插入图片描述


一行代码生成渐变色

https://github.com/xjh093/JHDraw


发布了201 篇原创文章 · 获赞 220 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/xjh093/article/details/104561358