无法使用UIGraphicsImageRenderer绘制阴影。

huangapple go评论78阅读模式
英文:

Not able to draw shadows using UIGraphicsImageRenderer

问题

我正在尝试使用UIGraphicsImageRenderer将UIView转换为UIImage。我已经在我的UIView上应用了阴影,但在输出图像中阴影被移除了。
以下是我的代码:

let view = UIView(frame: .init(origin: .zero, size: compressedImage.size))
view.backgroundColor = .lightGray
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowRadius = 5
view.layer.shadowOpacity = 0.5
view.layer.shadowOffset = .init(width: 2, height: 2)

let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
let rendereredImage = renderer.image { ctx in
     view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}

如果您将此rendereredImage分配给UIImageView,您会发现没有阴影。您能告诉我发生了什么吗?

英文:

I am trying convert an UIView into an UIImage using UIGraphicsImageRenderer. I have applied shadows on my UIView but shadows gets removed in output image.
Here's my code:

let view = UIView(frame: .init(origin: .zero, size: compressedImage.size))
view.backgroundColor = .lightGray
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowRadius = 5
view.layer.shadowOpacity = 0.5
view.layer.shadowOffset = .init(width: 2, height: 2)

let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
let rendereredImage = renderer.image { ctx in
     view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}

if you assign this rendereredImage to an UIImageView you can see that there are no shadows.
can you tell me what's happening ?

答案1

得分: 1

当你添加阴影时,视图的边界不会改变。阴影出现在视图边界之外。如果图像的大小是 view.bounds,那么阴影将被裁剪。

修复这个问题的简单方法是将视图放在稍大的父视图中:

let view = UIView(frame: .init(origin: .init(x: 2, y: 2), size: compressedImage.size))
view.backgroundColor = .lightGray
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowRadius = 5
view.layer.shadowOpacity = 0.5
view.layer.shadowOffset = .init(width: 2, height: 2)

let superView = UIView(frame: .init(
    x: 0, y: 0, 
    width: compressedImage.size.width + 5, 
    height: compressedImage.size.height + 5
))
superview.backgroundColor = .clear
superView.addSubview(view)

let renderer = UIGraphicsImageRenderer(size: superView.bounds.size)
let rendereredImage = renderer.image { ctx in
    superView.drawHierarchy(in: superView.bounds, afterScreenUpdates: true)
}

或者,考虑使用 view.layer.render(in:)

let renderer = UIGraphicsImageRenderer(size: view.bounds.insetBy(dx: -5, dy: -5).size)
let rendereredImage = renderer.image { ctx in
    view.layer.render(in: ctx.cgContext)
}
英文:

When you add a shadow, the bounds of the view do not change. The shadow appears outside of the bounds of the view. If the size of the image is view.bounds, then the shadow will be cut off.

A simple way to fix this is to put the view inside a slightly larger superview:

let view = UIView(frame: .init(origin: .init(x: 2, y: 2), size: compressedImage.size))
view.backgroundColor = .lightGray
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowRadius = 5
view.layer.shadowOpacity = 0.5
view.layer.shadowOffset = .init(width: 2, height: 2)

let superView = UIView(frame: .init(
    x: 0, y: 0, 
    width: compressedImage.size.width + 5, 
    height: compressedImage.size.height + 5
))
superview.backgroundColor = .clear
superView.addSubview(view)

let renderer = UIGraphicsImageRenderer(size: superView.bounds.size)
let rendereredImage = renderer.image { ctx in
    superView.drawHierarchy(in: superView.bounds, afterScreenUpdates: true)
}

Alternatively, consider using view.layer.render(in:).

let renderer = UIGraphicsImageRenderer(size: view.bounds.insetBy(dx: -5, dy: -5).size)
let rendereredImage = renderer.image { ctx in
    view.layer.render(in: ctx.cgContext)
}

答案2

得分: -1

如果您无法使用UIGraphicsImageRenderer绘制阴影,可能是您未设置正确的阴影属性,或者您的绘图代码未正确配置渲染器以绘制阴影。

以下是一些要检查的事项:

  1. 确保正确设置阴影属性。关键的阴影属性包括shadowOffsetshadowOpacityshadowRadiusshadowColor。您应该在绘制内容之前在CGContext上设置这些属性。例如:
context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)
  1. 检查是否以正确的顺序绘制具有阴影的内容。阴影应该首先绘制,然后才是它应该投射阴影的任何其他内容。投射阴影的内容应该绘制在阴影的顶部。例如:
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 100, height: 100))
let image = renderer.image { context in
    // 首先绘制阴影
    context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)
    context.fill(CGRect(x: 10, y: 10, width: 80, height: 80))

    // 在阴影的顶部绘制投射阴影的内容
    context.setFillColor(UIColor.red.cgColor)
    context.fill(CGRect(x: 20, y: 20, width: 60, height: 60))
}
  1. 确保您使用支持阴影的上下文。UIGraphicsImageRenderer默认使用支持阴影的上下文,但其他上下文类型可能不支持阴影。如果您使用自定义上下文,请确保它配置为支持阴影。例如:
let context = UIGraphicsGetCurrentContext()!
context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)

这些是使用UIGraphicsImageRenderer绘制阴影时可能导致阴影无法正确绘制的一些常见问题。如果您仍然遇到问题,请提供更多关于您的代码和您想要实现的内容的详细信息,我可以提供更具体的建议。

英文:

If you're not able to draw shadows using UIGraphicsImageRenderer, it's possible that you're not setting the appropriate shadow properties or that your drawing code is not correctly configuring the renderer to draw shadows.

Here are some things to check:

  1. Make sure you're setting the shadow properties correctly. The key shadow properties are shadowOffset, shadowOpacity, shadowRadius, and shadowColor. You should set these properties on the CGContext before drawing your content. For example:
context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)
  1. Check that you're drawing the shadowed content in the correct order. The shadow should be drawn first, before any other content that it should cast a shadow on. The content that casts the shadow should be drawn on top of the shadow. For example:
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 100, height: 100))
let image = renderer.image { context in
    // Draw the shadow first
    context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)
    context.fill(CGRect(x: 10, y: 10, width: 80, height: 80))

    // Draw the content that casts the shadow on top of the shadow
    context.setFillColor(UIColor.red.cgColor)
    context.fill(CGRect(x: 20, y: 20, width: 60, height: 60))
}
  1. Make sure you're using a context that supports shadows. The UIGraphicsImageRenderer uses a context that supports shadows by default, but other context types may not support shadows. If you're using a custom context, make sure that it's configured to support shadows. For example:
let context = UIGraphicsGetCurrentContext()!
context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)

These are some common issues that can prevent shadows from being drawn correctly using UIGraphicsImageRenderer. If you're still having trouble, please provide more details about your code and what you're trying to achieve, and I can provide more specific advice.

huangapple
  • 本文由 发表于 2023年7月24日 16:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定