-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Player add blur background #68
Comments
Same as here. But if you need animated background you should not specify single image as a background, rather blurring sourceImage.composited(over: sourceImage.applyBlur())
|
And this is possible implementation for image blaring if that is what you needed (it is not clear from your questions): extension CIImage {
convenience init(color: UIColor) {
self.init(color: CIColor(cgColor: color.cgColor))
}
enum Filter {
case
darkenBlendMode(UIColor),
gaussianBlur(radius: Double),
overlayBlendMode(CIImage)
var name: String {
switch self {
case .darkenBlendMode:
return "CIDarkenBlendMode"
case .gaussianBlur:
return "CIGaussianBlur"
case .overlayBlendMode:
return "CIOverlayBlendMode"
}
}
var parameters: [String: Any] {
switch self {
case .darkenBlendMode(let color):
return [kCIInputBackgroundImageKey: CIImage(color: color)]
case .gaussianBlur(let radius):
return [kCIInputRadiusKey: radius]
case .overlayBlendMode(let image):
return [kCIInputBackgroundImageKey: image]
}
}
}
func applying(_ filter: Filter, when shouldApply: Bool = true) -> CIImage {
guard shouldApply else { return self }
return applyingFilter(filter.name, parameters: filter.parameters)
}
}
extension CIImage {
func blurred(
radius: Double = 20,
color: UIColor = UIColor.white.withAlphaComponent(0.4)
) -> CIImage {
applying(
.overlayBlendMode(CIImage(
color: CIColor(
red: color.rgba().r,
green: color.rgba().g,
blue: color.rgba().b,
alpha: color.rgba().a
)
))
)
.applying(.gaussianBlur(radius: radius))
}
}
extension UIColor {
func rgba() -> (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) {
let components = cgColor.components ?? []
switch cgColor.numberOfComponents {
case 4:
return (components[0], components[1], components[2], components[3])
case 2:
return (components[0], components[0], components[0], components[1])
default:
return (0, 0, 0, 1)
}
}
} |
I tried the Blurred method and it didn't work |
I think you just don't see it, because blurred image is hidden underneath the source image. I'm not a guru in func applyEffect(
to sourceImage: CIImage,
at time: CMTime,
renderSize: CGSize
) -> CIImage {
let scaleToFillValue = renderSize.height / sourceImage.extent.height
let scaledImage = sourceImage.transformed(by: .init(scaleX: scaleToFillValue, y: scaleToFillValue))
let scaledImageWithFixedOrigin = scaledImage.transformed(
by: .init(
translationX: -renderSize.width / 2,
y: -(scaledImage.extent.origin.y + renderSize.height) / 2
)
)
return sourceImage.composited(over: scaledImageWithFixedOrigin.blurred(radius: 50, color: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.2)))
} |
Thank you for your reply. It is indeed blocked by the source image. I changed the position of the blurred image with the method you mentioned, and the effect is great! |
Here is an example
CIImage extension for
And here is example of usage:
NOTE: When we use custom |
I'm trying to do
The text was updated successfully, but these errors were encountered: