iOS混合模式倍增

Fra*_*eye 7 user-interface uiview uiimageview uiimage ios

我正在寻找一个解决方案来创建这个红色框:

在此输入图像描述

它使用的是'Multiply'滤镜.目前我已经找到了这些信息:https: //developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html

但是我如何使用UIView上的乘法效果之类的东西,或者这不可能吗?因此背景是UIImageView,红色框是具有乘法效果的UIView.

peg*_*peg 4

这个 Swift 扩展对我来说很有效。

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

   }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特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!

}
Run Code Online (Sandbox Code Playgroud)

但仅适用于图像,我也想为视频工作:|