2015-04-09 22 views

cevap

0

Bu Swift uzantısı benim için hile yaptı.

extension UIImage { 

//creates a static image with a color of the requested size 
static func fromColor(color: UIColor, size: CGSize) -> UIImage { 
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
    UIGraphicsBeginImageContext(rect.size) 
    let context = UIGraphicsGetCurrentContext() 
    CGContextSetFillColorWithColor(context, color.CGColor) 
    CGContextFillRect(context, rect) 
    let img = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return img 
} 


func blendWithColorAndRect(blendMode: CGBlendMode, color: UIColor, rect: CGRect) -> UIImage { 

    let imageColor = UIImage.fromColor(color, size:self.size) 

    let rectImage = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) 

    UIGraphicsBeginImageContextWithOptions(self.size, true, 0) 
    let context = UIGraphicsGetCurrentContext() 

    // fill the background with white so that translucent colors get lighter 
    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor) 
    CGContextFillRect(context, rectImage) 

    self.drawInRect(rectImage, blendMode: .Normal, alpha: 1) 
    imageColor.drawInRect(rect, blendMode: blendMode, alpha: 1) 

    // grab the finished image and return it 
    let result = UIGraphicsGetImageFromCurrentImageContext() 

    //self.backgroundImageView.image = result 
    UIGraphicsEndImageContext() 
    return result 

    } 
} 

Swift 3:

static func fromColor(color: UIColor, size: CGSize) -> UIImage { 
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
    UIGraphicsBeginImageContext(rect.size) 
    let context = UIGraphicsGetCurrentContext() 
    context!.setFillColor(color.cgColor) 
    context!.fill(rect) 
    let img = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return img! 
} 


func blendWithColorAndRect(blendMode: CGBlendMode, color: UIColor, rect: CGRect) -> UIImage { 

    let imageColor = UIImage.fromColor(color: color, size:self.size) 

    let rectImage = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) 

    UIGraphicsBeginImageContextWithOptions(self.size, true, 0) 
    let context = UIGraphicsGetCurrentContext() 

    // fill the background with white so that translucent colors get lighter 
    context!.setFillColor(UIColor.white.cgColor) 
    context!.fill(rectImage) 

    self.draw(in: rectImage, blendMode: .normal, alpha: 1) 
    imageColor.draw(in: rect, blendMode: blendMode, alpha: 1) 

    // grab the finished image and return it 
    let result = UIGraphicsGetImageFromCurrentImageContext() 

    //self.backgroundImageView.image = result 
    UIGraphicsEndImageContext() 
    return result! 

} 

Ama sadece görüntüler için çalışır, ben de videolar için çalışmak istiyorum: |