图片大小跟 UIImageView 的大小是一样的
代码如下:
extension UIImage {
func thumbnails(with options: SomeDrawingOptions) -> UIImage? {
// ...
UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0)
defer {
UIGraphicsEndImageContext()
}
let context = UIGraphicsGetCurrentContext()!
UIColor.white.setFill()
context.fill(targetRect)
UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip()
// Drawing image
draw(in: targetRect)
// Drawing a transparent mask
UIColor.black.withAlphaComponent(0.4).setFill()
context.fill(targetRect)
// Drawing bar
UIColor.white.setFill()
context.fill(someBarRect)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
截图:
UIGraphicsBeginImageContextWithOptions(targetRect.size, true, 0)