Ios 一种更有效的模糊视图小部分的方法?

Ios 一种更有效的模糊视图小部分的方法?,ios,uiview,swift3,uigraphicscontext,Ios,Uiview,Swift3,Uigraphicscontext,我想捕捉屏幕上一个小区域的快照,并对其应用模糊效果(尽管速度为60fps),这在以下代码中是不可能的: let rectangle = CGRect(x: -37, y: -153, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) captureSnapShotWithGaussianBlur(rect: rectangle) public func captureSnapShotWithGaus

我想捕捉屏幕上一个小区域的快照,并对其应用模糊效果(尽管速度为60fps),这在以下代码中是不可能的:

let rectangle = CGRect(x: -37, y: -153, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

captureSnapShotWithGaussianBlur(rect: rectangle)

public func captureSnapShotWithGaussianBlur(rect: CGRect) -> UIImage {

      let SCREEN_SIZE_SCALE_FACTOR = UIScreen.main.scale
      let CROPPING_RECT = CGRect(x: 0, y: 0, width: 118*SCREEN_SIZE_SCALE_FACTOR, height: 66*SCREEN_SIZE_SCALE_FACTOR)

      UIGraphicsBeginImageContextWithOptions(CGSize(width: 118, height: 66), true, 0.0)

      backgroundView!.drawHierarchy(in: rect, afterScreenUpdates: true)

      let capturedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
      let ciimage: CIImage = CIImage(image: capturedImage)!

      UIGraphicsEndImageContext()

      let gaussianfilter: CIFilter = CIFilter(name:"CIGaussianBlur")!
      gaussianfilter.setDefaults()
      gaussianfilter.setValue(ciimage, forKey: kCIInputImageKey)
      gaussianfilter.setValue(10, forKey: kCIInputRadiusKey)

      let outputImage: CIImage = gaussianfilter.outputImage!
      let finalImage: UIImage = UIImage(ciImage: outputImage.cropping(to: CROPPING_RECT), scale: SCREEN_SIZE_SCALE_FACTOR, orientation: UIImageOrientation.up)

      return finalImage

}
我在代码中看到的一个问题是,我认为高斯模糊应用于整个图像(因为用于drawHierarchy矩形的UIScreen.main.bounds)

我希望有一种方法可以对屏幕进行调整大小的NapshotView(),然后在该视图中进行drawHierarchy并提取图像-唉,这不起作用(虽然在模拟器中似乎是这样,但在手机上它只是呈现黑色)

关于如何制作更高性能的捕获和模糊方法,有什么建议吗


干杯

尝试使用此扩展快照视图并使其模糊,它似乎可以在模拟器和设备上工作:

extenstion UIView {
    /// A more efficient way to snapshot a UIView other than UIView.snapshot
///
/// - Returns: UIImage representing the snapshot of the UIView
public func snapshotImage() -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
    drawHierarchy(in: bounds, afterScreenUpdates: false)
    let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return snapshotImage
}

public func snapshotView() -> UIView? {
    if let snapshotImage = snapshotImage() {
        return UIImageView(image: snapshotImage)
    } else {
        return nil
    }
}
}

尝试使用此扩展对视图进行快照和模糊,它似乎可以在模拟器和设备上工作:

extenstion UIView {
    /// A more efficient way to snapshot a UIView other than UIView.snapshot
///
/// - Returns: UIImage representing the snapshot of the UIView
public func snapshotImage() -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
    drawHierarchy(in: bounds, afterScreenUpdates: false)
    let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return snapshotImage
}

public func snapshotView() -> UIView? {
    if let snapshotImage = snapshotImage() {
        return UIImageView(image: snapshotImage)
    } else {
        return nil
    }
}
}

当您提到先尝试快照时,您是否使用内置快照功能?是的,我尝试了许多方法组合,使用内置快照方法,然后尝试使用其内容创建图像以进行模糊处理,但都没有效果。快照方法在模拟器中运行良好,但在实际手机上呈现黑色。苹果-在文档中-说使用drawHierarchy应用模糊效果等。然而,我只想能够选择屏幕的一个区域,并在不到1/60秒的时间内对其应用模糊。当你提到要先尝试快照时,你是否使用内置快照功能?是的,我尝试了许多使用内置快照方法的方法组合,然后尝试使用这些方法的内容来创建图像进行模糊处理,但没有效果。快照方法在模拟器中运行良好,但在实际手机上呈现黑色。苹果-在文档中-说使用drawHierarchy应用模糊效果等。然而,我只想能够选择屏幕的一个区域,并在不到1/60秒的时间内对其应用模糊。感谢Sean,虽然这比我的代码可重用得多,但是否存在影响性能的实质性差异?从我所看到的情况来看,两者都调用完全相同的方法,并且都在做相同的事情——没有更高的性能。我应该用更高效的方法,而不是更高效的方法。再次感谢。感谢Sean,虽然这比我的代码更具可重用性,但是否存在影响性能的实质性差异?从我所看到的情况来看,两者都调用完全相同的方法,并且都在做相同的事情——没有更高的性能。我应该用更高效的方法,而不是更高效的方法。再次感谢。