iOS Swift create UIImage in Gradient color example

iOS Swift create UIImage in Gradient color example


import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "Main View"
        
        //Original image displayed as is
        let imageView = UIImageView()
        imageView.frame = CGRect(x: 50, y: 50, width: 200, height: 200)
        imageView.contentMode = .scaleAspectFit
        view.addSubview(imageView)
        
        let myImageName = "apple_logo.png"
        let myImage = UIImage(named: myImageName)
        imageView.image = myImage
        
        //Let's change the image to Gradient Color
        let imageView2 = UIImageView()
        imageView2.frame = CGRect(x: 300, y: 50, width: 200, height: 200)
        imageView2.contentMode = .scaleAspectFit
        view.addSubview(imageView2)
        
        let myImageName2 = "apple_logo.png"
        let myImage2 = UIImage(named: myImageName2)
        imageView2.image = myImage2!.maskWithGradientColor(color: UIColor.red)
                
    }
    
    
}

extension UIImage {
    func maskWithGradientColor(color: UIColor) -> UIImage? {
        
        let maskImage = self.cgImage
        let width = self.size.width
        let height = self.size.height
        let bounds = CGRect(x: 0, y: 0, width: width, height: height)
        
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        let bitmapContext = CGContext(data: nil,
                                      width: Int(width),
                                      height: Int(height),
                                      bitsPerComponent: 8,
                                      bytesPerRow: 0,
                                      space: colorSpace,
                                      bitmapInfo: bitmapInfo.rawValue)
        
        let locations:[CGFloat] = [0.0, 1.0]
        let bottom = UIColor(red: 1, green: 0, blue: 0, alpha: 1).cgColor
        let top = UIColor(red: 0, green: 1, blue: 0, alpha: 0).cgColor
        let colors = [top, bottom] as CFArray
        let gradient = CGGradient(colorsSpace: colorSpace, colors: colors, locations: locations)
        let startPoint = CGPoint(x: width/2, y: 0)
        let endPoint = CGPoint(x: width/2, y: height)
        
        bitmapContext!.clip(to: bounds, mask: maskImage!)
        bitmapContext!.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: CGGradientDrawingOptions(rawValue: UInt32(0)))
        
        if let cImage = bitmapContext!.makeImage() {
            let coloredImage = UIImage(cgImage: cImage)
            return coloredImage
        }
        else  {
            return nil
        }
    }
}


No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.