英文:
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
绘制阴影,可能是您未设置正确的阴影属性,或者您的绘图代码未正确配置渲染器以绘制阴影。
以下是一些要检查的事项:
- 确保正确设置阴影属性。关键的阴影属性包括
shadowOffset
、shadowOpacity
、shadowRadius
和shadowColor
。您应该在绘制内容之前在CGContext
上设置这些属性。例如:
context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)
- 检查是否以正确的顺序绘制具有阴影的内容。阴影应该首先绘制,然后才是它应该投射阴影的任何其他内容。投射阴影的内容应该绘制在阴影的顶部。例如:
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))
}
- 确保您使用支持阴影的上下文。
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:
- Make sure you're setting the shadow properties correctly. The key shadow properties are
shadowOffset
,shadowOpacity
,shadowRadius
, andshadowColor
. You should set these properties on theCGContext
before drawing your content. For example:
context.setShadow(offset: CGSize(width: 2, height: 2), blur: 4, color: UIColor.gray.cgColor)
- 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))
}
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论