iOS混合模式乘法 [英] iOS blend mode multiply

查看:27
本文介绍了iOS混合模式乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找创建此红框的解决方案:

I'm looking for a solution to create this red box:

它正在使用滤色器乘法".目前我找到了这个信息:https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html

It is using a color filter 'Multiply'. Currently I have found this information: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html

但是我怎样才能在 UIView 上使用乘法效果之类的东西,或者这不可能吗?所以背景是一个UIImageView,红框是一个有倍增效果的UIView.

But how can I use something like the multiply effect on a UIView or isn't this possible? So that the background is a UIImageView and the red box is a UIView with the multiply effect.

推荐答案

这个 Swift 扩展帮我解决了问题.

This Swift extension did the trick for me.

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!

}

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

But only works for images, I would like to work for videos too :|

这篇关于iOS混合模式乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆