简体   繁体   English

将arkit快照直接保存到照片库swift 4

[英]save arkit snapshot directly to photo gallery swift 4

The part of my code below takes a snapshot of the arkit and places it on a image view. 下面我的代码部分拍摄了arkit的快照并将其放在图像视图上。 I am trying to cut that out and and go from taking a photo and automatically saving it to the photo gallery without the need for placing it on a image view first. 我正试图将其剪掉并从拍摄照片并自动将其保存到照片库,而无需先将其放在图像视图上。

    @IBOutlet var cryMeARiver: UIImageView!
@IBOutlet weak var augmentedRealityView: ARSCNView!

 @IBAction func place(_ sender: Any) {
//THIS LINE OF THE CODE BELOW SAVES THE SNAPSHOT TO THE IMAGEVIEW
cryMeARiver.image = augmentedRealityView.snapshot()
}

Since the snapshot function returns a UIImage it is very easy to save it to the IOS Photo Library . 由于snapshot功能返回UIImage ,因此很容易将其保存到IOS Photo Library

Before you begin, you need to ensure that in your info.plist you have set the following key: 在开始之前,您需要确保在info.plist中设置了以下密钥:

  <key>NSPhotoLibraryUsageDescription</key>
  <string>To Save Your ARKit Scenes</string>

Then you can call your function like so: 然后你可以像这样调用你的函数:

 @IBAction func saveScreenhot(){

        //1. Create A Snapshot
        let snapShot = self.augmentedRealityView.snapshot()

        //2. Save It The Photos Album
        UIImageWriteToSavedPhotosAlbum(snapShot, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

 }

Remembering of course to use the callback method to check whether the process was successful or not: 当然要记住使用回调方法来检查进程是否成功:

@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {

    if let error = error {
        print("Error Saving ARKit Scene \(error)")
    } else {
        print("ARKit Scene Successfully Saved")
    }
}

Hope it helps... 希望能帮助到你...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM