gpt4 book ai didi

swift - 使用 Swift 将 Image 上下文设置为 ScreenShot 的 subview

转载 作者:行者123 更新时间:2023-11-30 11:48:08 24 4
gpt4 key购买 nike

我想捕获属于堆栈 View 一部分的 View 中的图像(屏幕截图)(这里是ResultsView)。因为我使用堆栈 View 来重新定位两个 View ,所以我需要使用 ResultsView View 来捕获图像(即我无法使用屏幕上的地理位置)。

我最终的结果要么是整体,要么是部分,使用一些屏幕位置:

    // full screen shot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let sourceImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//print("ScreenShot")
//UIImageWriteToSavedPhotosAlbum(sourceImage!, nil, nil, nil)

// partial screen shot
UIGraphicsBeginImageContext(view.frame.size)
sourceImage?.draw(at: CGPoint(x: 0, y: 275))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
print("Partial ScreenShot")
UIImageWriteToSavedPhotosAlbum(croppedImage!, nil, nil, nil)

swift 4

view hierarchy

最佳答案

回答我自己的问题...

@IBAction func screenShot(_ sender: RoundButton) {
sender.pulsate()
makeSystemSound(sound: 1108) // camera shutter
//screenshot with cropping to CGRect from a view
UIGraphicsBeginImageContext(view.frame.size)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let sourceImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let captureFrame = view.convert(ResultsView.frame, from:self.view)
let newImage = crop(image: sourceImage!, cropRect: captureFrame)
print("Partial ScreenShot")
UIImageWriteToSavedPhotosAlbum(newImage!, nil, nil, nil)
}

和函数:

func crop(image: UIImage, cropRect: CGRect) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(cropRect.size, false, image.scale)
let origin = CGPoint(x: cropRect.origin.x * CGFloat(-1), y: cropRect.origin.y * CGFloat(-1))
image.draw(at: origin)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return result
}

关于swift - 使用 Swift 将 Image 上下文设置为 ScreenShot 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48601239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com