【问题标题】:add a drop shadow on an UIImage, not UIImageView在 UIImage 上添加阴影,而不是 UIImageView
【发布时间】:2021-11-06 14:10:44
【问题描述】:

我需要在我的图像上添加阴影,而不是图像视图。有没有办法做到这一点?我知道我可以向 imageView 添加阴影 -

imageView.layer.masksToBounds true
imageView.layer.shadowColor = UIColor.gray.cgColor
imageView.layer.shadowOffset = CGSizeMake(0, 1)
imageView.layer.shadowOpacity = 1
imageView.layer.shadowRadius = 1.0

但我需要将阴影添加到图像,而不是 imageView。有人知道吗?

【问题讨论】:

  • 类似this?
  • 有趣的@MadPogrammer。希望有办法说您的评论没有有帮助。您链接到 2010 年 - 11 年前的一个问题 - 有 8 个答案,接受的答案也来自 2010 年并使用 Obj-C。对你真的没有帮助。
  • 听起来可能有与您要求的不同的解决方案。您能否添加一个示例,说明您拥有的图像以及您希望它的外观?
  • @MadProgrammer 向您提供这个问题是绝对正确的。你可以找到this answer,它来自 2020 年,具有现代的 swift 语法。以防万一你以后遇到只有 ObjC 才有的答案,我建议你学习将代码从 ObjC 转换为 Swift 的技巧。这并不难,答案也不会失去相关性,因为所有 UIKit 仍然是用 ObjC 编写的,而在 Swift 中我们只使用了一个包装器。

标签: ios swift shadow


【解决方案1】:

我认为你可以使用CIFilter in Core Image。 CIFilter 是一种图像处理器,它通过处理一个或多个输入图像或生成新的图像数据来生成图像。

您可以查看各种参考资料here.

我想你可以CIHighlightShadowAdjust CIHighlightShadowAdjust 是用于配置高光-阴影调整滤镜的属性。

【讨论】:

    【解决方案2】:

    只为@dfd。

    所以,我去看了Create new UIImage by adding shadow to existing UIImage。向下滚动了一下之后,我开始找到几个基于 Swift 的解决方案。出于好奇,我把它们扔进了游乐场,看看它们能做什么。

    我选择了这个解决方案...

    import UIKit
    
    extension UIImage {
        /// Returns a new image with the specified shadow properties.
        /// This will increase the size of the image to fit the shadow and the original image.
        func withShadow(blur: CGFloat = 6, offset: CGSize = .zero, color: UIColor = UIColor(white: 0, alpha: 0.8)) -> UIImage {
    
            let shadowRect = CGRect(
                x: offset.width - blur,
                y: offset.height - blur,
                width: size.width + blur * 2,
                height: size.height + blur * 2
            )
            
            UIGraphicsBeginImageContextWithOptions(
                CGSize(
                    width: max(shadowRect.maxX, size.width) - min(shadowRect.minX, 0),
                    height: max(shadowRect.maxY, size.height) - min(shadowRect.minY, 0)
                ),
                false, 0
            )
            
            let context = UIGraphicsGetCurrentContext()!
    
            context.setShadow(
                offset: offset,
                blur: blur,
                color: color.cgColor
            )
            
            draw(
                in: CGRect(
                    x: max(0, -shadowRect.origin.x),
                    y: max(0, -shadowRect.origin.y),
                    width: size.width,
                    height: size.height
                )
            )
            let image = UIGraphicsGetImageFromCurrentImageContext()!
            
            UIGraphicsEndImageContext()
            return image
        }
    }
    
    let sourceImage = UIImage(named: "LogoSmall.png")!
    
    let shadowed = sourceImage.withShadow(blur: 6, color: .red)
    

    但是等等,那不是阴影,而是轮廓!

    ? 显然我们现在需要牵手每个人......

    将参数更改为 ...

    let shadowed = sourceImage.withShadow(blur: 6, offset: CGSize(width: 5, height: 5), color: .red)
    

    产生投影。我喜欢这个解决方案,因为它不做任何假设,并提供适当数量的参数来根据需要更改输出。

    我非常喜欢这个解决方案,我将扩展复制到我的个人库中,不知道什么时候会派上用场。

    记住,为了产生这种风格的图像,原始图像需要是透明的。

    有点像……

    请记住,iOS 已经存在了很长时间,ObjC 已经存在了更长的时间。您可能会遇到许多仅在 ObjC 中提供的解决方案,这意味着拥有至少阅读代码的技能/能力仍然很重要。如果幸运的话,社区的其他成员会产生合适的 Swift 变体,但这并不总是可能的。

    我确定我不需要写一篇关于如何在 Playground 中包含图像的完整教程的程度,有 plenty of examples 关于那个?

    【讨论】:

      猜你喜欢
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 2011-02-25
      相关资源
      最近更新 更多