的过程如下:
import UIKit
func createCircularImage(size: CGSize) -> UIImage {
// ...
}
func createCircularImage(size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { context in
// ...
}
return image
}
func createCircularImage(size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { context in
let rect = CGRect(origin: .zero, size: size)
let path = UIBezierPath(ovalIn: rect)
// ...
}
return image
}
func createCircularImage(size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { context in
let rect = CGRect(origin: .zero, size: size)
let path = UIBezierPath(ovalIn: rect)
UIColor.red.setFill() // 设置填充颜色
path.fill() // 填充路径
}
return image
}
func createCircularImage(size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { context in
let rect = CGRect(origin: .zero, size: size)
let path = UIBezierPath(ovalIn: rect)
UIColor.red.setFill() // 设置填充颜色
path.fill() // 填充路径
}
return image
}
使用示例:
let circularImage = createCircularImage(size: CGSize(width: 100, height: 100))
imageView.image = circularImage
这样就可以使用UIGraphicsImageRenderer创建一个指定大小的圆形图像了。在实际开发中,可以根据具体需求进一步定制和优化这个函数,并根据不同的场景选择合适的颜色、大小和位置来创建图像。
领取专属 10元无门槛券
手把手带您无忧上云