- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题 : 在我截取屏幕截图后,通过缩放检查时图像模糊。放大时图像内的文字似乎模糊不清。
我知道这个问题已经被提出很多次了,但他们都没有想要的解决方案。我已经检查了很多帖子,例如 this one
迄今为止在此论坛上共享的所有解决方案都以任何其他方式重复或相同,但没有一个解决方案可以解决问题。
这是我在做什么:
extension UIView {
func asImage() -> UIImage? {
let format = UIGraphicsImageRendererFormat()
format.opaque = self.isOpaque
let renderer = UIGraphicsImageRenderer(bounds: bounds,format: format)
return renderer.image(actions: { rendererContext in
layer.render(in: rendererContext.cgContext)
})
}
//The other option using UIGraphicsEndImageContext
func asImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
defer { UIGraphicsEndImageContext() }
if let context = UIGraphicsGetCurrentContext() {
self.layer.render(in: context)
return UIGraphicsGetImageFromCurrentImageContext()
}
return nil
}
}
上述函数将 UIView 转换为图像,但返回的图像质量达不到标准。
最佳答案
执行 UIView
不会得到您想要的结果“图像捕捉。”
当您缩放 UIScrollView
确实如此 不是 执行矢量缩放...它执行光栅化缩放。
您可以使用 UILabel
轻松确认这一点。如 viewForZooming
.这是一个带有 30 磅系统字体的标签...
在 1x
飞涨:
在 10x
飞涨:
该示例的代码:
class ViewController: UIViewController, UIScrollViewDelegate {
let zoomLabel: UILabel = UILabel()
let scrollView: UIScrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
[zoomLabel, scrollView].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
}
scrollView.addSubview(zoomLabel)
view.addSubview(scrollView)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
scrollView.centerXAnchor.constraint(equalTo: g.centerXAnchor),
scrollView.centerYAnchor.constraint(equalTo: g.centerYAnchor),
scrollView.widthAnchor.constraint(equalToConstant: 300.0),
scrollView.heightAnchor.constraint(equalToConstant: 200.0),
zoomLabel.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
zoomLabel.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
zoomLabel.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
zoomLabel.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
])
zoomLabel.textColor = .red
zoomLabel.backgroundColor = .yellow
zoomLabel.font = UIFont.systemFont(ofSize: 30.0, weight: .regular)
zoomLabel.text = "Sample Text"
scrollView.delegate = self
scrollView.minimumZoomScale = 1
scrollView.maximumZoomScale = 10
view.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
scrollView.backgroundColor = .white
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return zoomLabel
}
}
当您“捕获 View 内容”作为
UIImage
,你会得到一个位图,它是 View 的大小,以点 x 屏幕比例为单位。
@2x
屏幕比例,在
300 x 200
View 将被“捕获”为
UIImage
与
600 x 400
像素。
3000 x 2000
的原始图像像素,显示在
300 x 200
点。添加一个 30 点的标签可能在屏幕上看起来不错,但随后将其抓取为
UIImage
(用于缩放或保存到磁盘),最终为
600 x 400
像素图像,当然,在更大的尺寸下看起来不太好。
3000 x 2000
像素图像按比例缩小 300 x 200
查看 (32, 32)
3000 x 2000
像素图像(32 * 10, 32 * 10)
300 x 200
ImageView ,与您的
3000 x 2000
像素图像(缩放以适合)。当用户说“我想在 rect
(20, 20, 200, 200)
中添加一个椭圆形贝塞尔路径”时,您的代码将在图像本身的 rect
(20 * 10, 20 * 10, 200 * 10, 200 * 10)
处绘制该椭圆,然后刷新 ImageView 的
.image
属性。
class ViewController: UIViewController, UIScrollViewDelegate {
let topView: UIView = UIView()
let topLabel: UILabel = UILabel()
let botView: UIView = UIView()
let botLabel: UILabel = UILabel()
let topScrollView: UIScrollView = UIScrollView()
let botScrollView: UIScrollView = UIScrollView()
let topStatLabel: UILabel = UILabel()
let botStatLabel: UILabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
[topView, topLabel, botView, botLabel, topScrollView, botScrollView, topStatLabel, botStatLabel].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
}
topView.addSubview(topLabel)
botView.addSubview(botLabel)
topScrollView.addSubview(topView)
botScrollView.addSubview(botView)
view.addSubview(topStatLabel)
view.addSubview(botStatLabel)
view.addSubview(topScrollView)
view.addSubview(botScrollView)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
topStatLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
topStatLabel.leadingAnchor.constraint(equalTo: topScrollView.leadingAnchor),
topScrollView.topAnchor.constraint(equalTo: topStatLabel.bottomAnchor, constant: 4.0),
topScrollView.centerXAnchor.constraint(equalTo: g.centerXAnchor),
topScrollView.widthAnchor.constraint(equalToConstant: 300.0),
topScrollView.heightAnchor.constraint(equalToConstant: 200.0),
botScrollView.topAnchor.constraint(equalTo: topScrollView.bottomAnchor, constant: 12.0),
botScrollView.centerXAnchor.constraint(equalTo: g.centerXAnchor),
botScrollView.widthAnchor.constraint(equalToConstant: 300.0),
botScrollView.heightAnchor.constraint(equalToConstant: 200.0),
botStatLabel.topAnchor.constraint(equalTo: botScrollView.bottomAnchor, constant: 4.0),
botStatLabel.leadingAnchor.constraint(equalTo: botScrollView.leadingAnchor),
topView.widthAnchor.constraint(equalToConstant: 300.0),
topView.heightAnchor.constraint(equalToConstant: 200.0),
botView.widthAnchor.constraint(equalToConstant: 300.0 * 10.0),
botView.heightAnchor.constraint(equalToConstant: 200.0 * 10.0),
topLabel.topAnchor.constraint(equalTo: topView.topAnchor, constant: 8.0),
topLabel.leadingAnchor.constraint(equalTo: topView.leadingAnchor, constant: 8.0),
botLabel.topAnchor.constraint(equalTo: botView.topAnchor, constant: 8.0 * 10.0),
botLabel.leadingAnchor.constraint(equalTo: botView.leadingAnchor, constant: 8.0 * 10.0),
topView.topAnchor.constraint(equalTo: topScrollView.contentLayoutGuide.topAnchor),
topView.leadingAnchor.constraint(equalTo: topScrollView.contentLayoutGuide.leadingAnchor),
topView.trailingAnchor.constraint(equalTo: topScrollView.contentLayoutGuide.trailingAnchor),
topView.bottomAnchor.constraint(equalTo: topScrollView.contentLayoutGuide.bottomAnchor),
botView.topAnchor.constraint(equalTo: botScrollView.contentLayoutGuide.topAnchor),
botView.leadingAnchor.constraint(equalTo: botScrollView.contentLayoutGuide.leadingAnchor),
botView.trailingAnchor.constraint(equalTo: botScrollView.contentLayoutGuide.trailingAnchor),
botView.bottomAnchor.constraint(equalTo: botScrollView.contentLayoutGuide.bottomAnchor),
])
topLabel.textColor = .red
topLabel.backgroundColor = .yellow
topLabel.font = UIFont.systemFont(ofSize: 30.0, weight: .regular)
topLabel.text = "Sample Text"
botLabel.textColor = .red
botLabel.backgroundColor = .yellow
botLabel.font = UIFont.systemFont(ofSize: 30.0 * 10.0, weight: .regular)
botLabel.text = "Sample Text"
topScrollView.delegate = self
topScrollView.minimumZoomScale = 1
topScrollView.maximumZoomScale = 10
botScrollView.delegate = self
botScrollView.minimumZoomScale = 0.1
botScrollView.maximumZoomScale = 1
topScrollView.zoomScale = topScrollView.minimumZoomScale
botScrollView.zoomScale = botScrollView.minimumZoomScale
view.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
topScrollView.backgroundColor = .white
botScrollView.backgroundColor = .white
topStatLabel.font = UIFont.systemFont(ofSize: 14, weight: .light)
topStatLabel.numberOfLines = 0
botStatLabel.font = UIFont.systemFont(ofSize: 14, weight: .light)
botStatLabel.numberOfLines = 0
let t = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
view.addGestureRecognizer(t)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateStatLabels()
}
func updateStatLabels() -> Void {
var sTop = ""
sTop += "Label Point Size: \(topLabel.font.pointSize)"
sTop += "\n"
sTop += "Label Frame: \(topLabel.frame)"
sTop += "\n"
sTop += "View Size: \(topView.bounds.size)"
sTop += "\n"
sTop += "Zoom Scale: \(String(format: "%0.1f", topScrollView.zoomScale))"
var sBot = ""
sBot += "Zoom Scale: \(String(format: "%0.1f", botScrollView.zoomScale))"
sBot += "\n"
sBot += "View Size: \(botView.bounds.size)"
sBot += "\n"
sBot += "Label Frame: \(botLabel.frame)"
sBot += "\n"
sBot += "Label Point Size: \(botLabel.font.pointSize)"
topStatLabel.text = sTop
botStatLabel.text = sBot
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
if scrollView == topScrollView {
return topView
}
return botView
}
@objc func tapped(_ g: UITapGestureRecognizer) -> Void {
if Int(topScrollView.zoomScale) == Int(topScrollView.maximumZoomScale) {
topScrollView.zoomScale = topScrollView.minimumZoomScale
} else {
topScrollView.zoomScale += 1
}
topScrollView.contentOffset = .zero
// comparing floating point directly will fail, so round the values
if round(botScrollView.zoomScale * 10) == round(botScrollView.maximumZoomScale * 10) {
botScrollView.zoomScale = botScrollView.minimumZoomScale
} else {
botScrollView.zoomScale += 0.1
}
botScrollView.contentOffset = .zero
updateStatLabels()
}
}
顶部 ScrollView 有一个
300 x 200
带有 30 点标签的 View ,允许从
1 to 10
缩放比例.
3000 x 2000
带有 300 点标签的 View ,允许从
0.1 to 1.0
缩放比例.
关于ios - 如何以编程方式使用 UIGraphicsImageRenderer 拍摄高质量的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65392607/
我面临以下问题:我有几个 UIImage(不是方形的),我需要调整它们的大小并裁剪它们。我已经阅读了关于 StackOverflow 的几乎所有问题,但我得到的结果并不好,我的意思是生成的图像质量很差
在查看了一些服务/工具之后,我得出了一个结论。大多数 Text-to-Speech 工具的技术含量太高、太机械化——换句话说,就是质量差的 c 语音。 是的,最重要的是,看起来它们带有“硬编码”语音模
我一直用pgfplot的 latex生成数字(.pdf 格式)。现在,我想发表的期刊希望我的数字为 eps 格式。 我尝试使用 pdftops -eps fig.pdf fig.eps 将 pdf 转
我有一个问题,我一直无法弄清楚。我有一个应用程序可以从正面和背面相机拍摄一张照片,然后将它们合并在一起。 应用的名称是GroupCam 但是我有一个问题,我的 UIButtons 从一开始就不会作为
我有兴趣创建一个非常简单、高质量(加密)的随 secret 码生成器。有没有更好的方法来做到这一点? import os, random, string length = 13 chars = str
我正在尝试通过 Imagick 将 PDF 转换为高质量的 PNG,但文件总是模糊不清。目前,我正在运行以下选项,但找不到正确的标志来从转换中获得清晰的 PNG。原始 PDF 文件为 8.5 x 11
当我运行这个程序时,图像将转换为 pdf,但给定的输出 pdf 与图像不匹配,因为它被裁剪了。我使用 iText 库。 import java.io.FileOutputStream;
simple 之间有什么区别? , highQuality和 balanced textBreakStrategy 的值的属性(property)React Native 中的组件? document
高质量的图像还需要下载(从 Firebase)并显示,一旦我们滚动并且图像超出屏幕,然后 feed 恢复正常,也会使 feed 缓慢且滞后 https://pastebin.com/ibcaGzVH这
我使用 FabricJs 在 Canvas 上创建了一个带有一些图片和文本的设计 (270x470),然后我通过 fabricJS 的 canvas.toJSON() 方法以 JSON 格式导出所有图
我是一名优秀的程序员,十分优秀!