ios – 如何从AV Foundation返回图像仍然捕获并将其添加到图形上下文中以获取屏幕截图?

ios – 如何从AV Foundation返回图像仍然捕获并将其添加到图形上下文中以获取屏幕截图?,第1张

概述我有一个相机预览,效果很好.我想截取它的屏幕截图及其上的所有内容.但是,由于通常的截图方法:CALayer的renderInContext不会从相机渲染内容,我需要单独添加它. 我在视图中使用此功能控制器捕获图像并将其保存到相机胶卷. @IBAction func snapStillImage(sender: AnyObject) { print("snapStillImage") @H_403_6@ 我有一个相机预览,效果很好.我想截取它的屏幕截图及其上的所有内容.但是,由于通常的截图方法:CALayer的renderInContext不会从相机渲染内容,我需要单独添加它.

我在视图中使用此功能控制器捕获图像并将其保存到相机胶卷.

@IBAction func snapStillimage(sender: AnyObject) {        print("snapStillimage")        (self.prevIEwVIEw.layer as! AVCaptureVIDeoPrevIEwLayer).connection.enabled=false;        dispatch_async(self.sessionQueue,{            // Update the orIEntation on the still image output vIDeo connection before capturing.            let vIDeoOrIEntation =  (self.prevIEwVIEw.layer as! AVCaptureVIDeoPrevIEwLayer).connection.vIDeoOrIEntation            self.stillimageOutput!.connectionWithMediaType(AVMediaTypeVIDeo).vIDeoOrIEntation = vIDeoOrIEntation            // Flash set to auto for Still Capture            VIEwController.setFlashMode(AVCaptureFlashMode.auto,device: self.vIDeoDeviceinput!.device)            self.stillimageOutput!.captureStillimageAsynchronouslyFromConnection(self.stillimageOutput!.connectionWithMediaType(AVMediaTypeVIDeo),completionHandler: {                (imageDataSampleBuffer: CMSampleBuffer!,error: NSError!) in                if error == nil {                    let data:NSData = AVCaptureStillimageOutput.jpegStillimageNSDataRepresentation(imageDataSampleBuffer)                    let image:UIImage = UIImage( data: data)!                    let libaray:ALAssetsLibrary = ALAssetsLibrary()                    let orIEntation: ALAssetorIEntation = ALAssetorIEntation(rawValue: image.imageOrIEntation.rawValue)!                    libaray.writeImagetoSavedPhotosAlbum(image.CGImage,orIEntation: orIEntation,completionBlock: nil)                    print("save to album")                }else{//                    print("DID not capture still image")                    print(error)                }            })        })    }

我想返回使用let image定义的图像:UIImage = UIImage(数据:数据)!
以便通过以下功能访问它

@IBAction func downloadbutton(sender: UIbutton) {    //Create the UIImage    UIGraphicsBeginImageContextWithOptions(prevIEwVIEw.frame.size,false,0.0)    prevIEwVIEw.layer.renderInContext(UIGraphicsGetCurrentContext()!)    //ALSO ADD image captured with captureStillimageAsynchronouslyFromConnection right here to the context.    let image = UIGraphicsGetimageFromCurrentimageContext()    //Save it to the camera roll    UIImageWritetoSavedPhotosAlbum(image,nil,nil)}

这将让我的屏幕截图包含最近捕获的图像以及添加了UIGraphicsGetCurrentContext()的所有内容!

我听说您通过将图像包含在captureStillimageAsynchronouslyFromConnection函数中已包含的完成处理程序块中来完成此 *** 作,但我不太清楚如何执行此 *** 作.有什么建议?

解决方法 您可以将捕获的静止图像添加为UIImage作为prevIEwVIEw的子视图.然后调用renderInContext.

假设这个代码都在同一个类中进行,你可以使image成为一个属性

class CameraviewController: UIVIEwController {    var image: UIImage!    ...

然后在captureStillimageAsynchronouslyFromConnection中

代替

let image:UIImage = UIImage( data: data)!

使用

self.image = UIImage( data: data)!

然后在downloadbutton中使用

var imageVIEw = UIImageVIEw(frame: prevIEwVIEw.frame)imageVIEw.image = self.imageprevIEwVIEw.addSubvIEw(imageVIEw)prevIEwVIEw.layer.renderInContext(UIGraphicsGetCurrentContext()!)

然后,您可以根据需要删除imageVIEw

imageVIEw.removeFromSupervIEw()
总结

以上是内存溢出为你收集整理的ios – 如何从AV Foundation返回图像仍然捕获并将其添加到图形上下文中以获取屏幕截图?全部内容,希望文章能够帮你解决ios – 如何从AV Foundation返回图像仍然捕获并将其添加到图形上下文中以获取屏幕截图?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1014159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存