2015-04-09 18 views
5

szukam rozwiązanie pozwalające na stworzenie tego czerwone pole:tryb mieszania iOS pomnożyć

enter image description here

Jest stosując filtr kolorów „Multiply”. Obecnie znalazłem tę informację: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html

Ale jak mogę użyć czegoś takiego jak efekt zwielokrotnienia na UIView czy nie jest to możliwe? Tak, że tło to UIImageView, a czerwone pole to UIView z efektem mnożenia.

Odpowiedz

0

To rozszerzenie Swift mi pomogło.

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! 

} 

ale działa tylko w przypadku zdjęć, chciałbym pracować dla filmów też: |