gpt4 book ai didi

iOS混合模式相乘

转载 作者:可可西里 更新时间:2023-11-01 03:32:16 24 4
gpt4 key购买 nike

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

enter image description here

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

但是我怎样才能在 UIView 上使用乘法效果之类的东西,或者这不可能吗?这样背景就是一个UIImageView,红框就是一个叠加效果的UIView。

最佳答案

这个 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

}
}

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!

}

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

关于iOS混合模式相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29541153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com