ios - A more efficient method of blurring a small section of a UIView? -
i wanting capture snapshot of small area of screen , apply blur - albeit @ 60fps - not possible code:
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 }
one of problems see code think gaussian blur applied whole image (because of uiscreen.main.bounds used drawhierarchy rectangle).
i hoping there way of doing resizablesnapshotview() of screen, doing drawhierarchy in view , extracting image - alas, not work (although seems in simulator, not on phone renders black).
any suggestions on how make more performant capture , blur method?
cheers!
try using extension snapshot view , blur this, seems work on both simulator , device:
extenstion uiview { /// more efficient way snapshot uiview other uiview.snapshot /// /// - returns: uiimage representing snapshot of 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 } } }
Comments
Post a Comment