반응형
iOS 15.5, Xcode 13.31, Swift 5, UIKit 환경에서 진행했습니다.
extension UIImage {
static func gradientImageWithBounds(bounds: CGRect, colors: [CGColor]) -> UIImage {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = colors
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
let image = UIImage.gradientImageWithBounds(bounds: CGRect(x: 0, y: 0, width: 200, height: 200), colors: [UIColor.yellow.cgColor, UIColor.blue.cgColor])
let imageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
imageView.image = image
반응형
'iOS > 코드조각' 카테고리의 다른 글
[iOS, Swift] UIImageView 원으로 만들기 (0) | 2022.08.21 |
---|---|
[iOS, Swift] UIImagePickerController 이미지 선택 화면 띄우기 (0) | 2022.08.21 |
[iOS, Swift] LocalNotification Foreground 알림 받기 (0) | 2022.08.21 |
[iOS, Swift] LocalNotification Badge 숫자 올리기 (0) | 2022.08.21 |
[iOS, Swift] LocalNotification 실행하기 (0) | 2022.08.21 |