作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有以下代码(在 UIView
的扩展中)将 UIView
分成一定数量的片段:
public func fragment(into numberOfFragments: Int) -> [UIView] {
var fragments = [UIView]()
guard let containerView = superview, let snapshot = snapshotView(afterScreenUpdates: true) else { return fragments }
let fragmentWidth = snapshot.frame.width / CGFloat(numberOfFragments)
let fragmentHeight = snapshot.frame.height / CGFloat(numberOfFragments)
for x in stride(from: 0.0, to: snapshot.frame.width, by: fragmentWidth) {
for y in stride(from: 0.0, to: snapshot.frame.height, by: fragmentHeight) {
let rect = CGRect(x: x, y: y, width: fragmentWidth, height: fragmentHeight)
if let fragment = snapshot.resizableSnapshotView(from: rect, afterScreenUpdates: true, withCapInsets: .zero) {
fragment.frame = convert(rect, to: containerView)
containerView.addSubview(fragment)
fragments.append(fragment)
}
}
}
return fragments
}
但是,对于 numberOfFragments=20
,此代码大约需要 2 秒才能完成。有什么方法可以更快地实现同样的结果吗? 我应该改用动画/过渡吗?
非常感谢。
最佳答案
此解决方案使用 UIImageViews 而不是 UIViews。它裁剪单个捕获的屏幕截图,而不是调用更昂贵的 resizableSnapshotView
400 次。如果 afterScreenUpdates
设置为 false(适用于我的测试用例),时间从 ~2.0 秒减少到 0.088 秒。如果您的目的需要 afterScreenUpdates
,则时间约为 0.15 秒。仍然 - 比 2.0 秒快得多!
public func fragment(into numberOfFragments: Int) -> [UIImageView] {
var fragments = [UIImageView]()
guard let containerView = superview else { return fragments }
let renderer = UIGraphicsImageRenderer(size: containerView.bounds.size)
let image = renderer.image { ctx in
containerView.drawHierarchy(in: containerView.bounds, afterScreenUpdates: false)
}
let fragmentWidth = containerView.frame.width / CGFloat(numberOfFragments)
let fragmentHeight = containerView.frame.height / CGFloat(numberOfFragments)
for x in stride(from: 0.0, to: containerView.frame.width, by: fragmentWidth) {
for y in stride(from: 0.0, to: containerView.frame.height, by: fragmentHeight) {
let rect = CGRect(x: x, y: y, width: fragmentWidth, height: fragmentHeight)
if let imageFrag = cropImage(image, toRect: rect) {
let fragment = UIImageView(image: imageFrag)
fragment.frame = convert(rect, to: containerView)
containerView.addSubview(fragment)
fragments.append(fragment)
}
}
}
return fragments
}
func cropImage(_ inputImage: UIImage, toRect cropRect: CGRect) -> UIImage?
{
let imageViewScale = UIScreen.main.scale
// Scale cropRect to handle images larger than shown-on-screen size
let cropZone = CGRect(x:cropRect.origin.x * imageViewScale,
y:cropRect.origin.y * imageViewScale,
width:cropRect.size.width * imageViewScale,
height:cropRect.size.height * imageViewScale)
// Perform cropping in Core Graphics
guard let cutImageRef: CGImage = inputImage.cgImage?.cropping(to:cropZone)
else {
return nil
}
// Return image to UIImage
let croppedImage: UIImage = UIImage(cgImage: cutImageRef)
return croppedImage
}
关于ios - 加速 View 的碎片化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51921982/
当使用 FFmpeg 控制台将 RTSP 流保存到分段 MP4 文件时,我注意到存在少量内存泄漏。 它每小时泄漏大约 3-4 MB(当每帧是一个片段和 30 fps 时)。 使用了以下命令: ffmp
我尝试了文档 http://doc.scrapy.org/en/stable/topics/practices.html 中的示例,但第二次运行时会出现错误ReactorNotRestartable。
I published my application to the Play Store as open beta. I don't get any error when I run it wi
我是一名优秀的程序员,十分优秀!