将completionSelector 和completionTarget 与UIImageWriteToSavedPhotosAlbum 结合使用

2024-02-13

我正在尝试找出一种方法让我的 iOS 应用程序将屏幕截图保存到相机胶卷,然后弹出警报告诉用户屏幕截图已成功保存。

我能想到的唯一方法是使用某种形式的 if/else 循环(正如您将在下面的伪代码注释中看到的那样),但我想不出任何语法来使用UIImageWriteToSavedPhotosAlbum来自 UIKit 的函数。我在Apple的开发网站上遇到过建议使用completionSelector和completionTarget,但我真的不明白如何使用它们或者我应该使用什么具体的措辞completionSelector and completionTarget在我的代码中。我对 Swift 还比较陌生。

有人可以解释它们是如何工作的以及我如何找到在我的代码中使用它们的语法吗?

func screenshotMethod()
{
    UIGraphicsBeginImageContextWithOptions(HighchartsView.scrollView.contentSize, false, 0);
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    //if saved to Camera Roll
    //            {
    //              confirmScreenshot()
    //            }
    //        
    //else 
    //        {
    //            exit code/stop 
    //        }


}


func confirmScreenshot()
{
    let alertController = UIAlertController(title: "Success", message: "This chart has been successfully saved to your Camera Roll.", preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    presentViewController(alertController, animated: true, completion: nil)
}

import UIKit

class ViewController: UIViewController {

   @IBAction func buttonPressed(sender: AnyObject) {
      presentImagePickerController()
   }

}

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

   func presentImagePickerController() {
      let imagePickerController = UIImagePickerController()
      imagePickerController.delegate = self
      imagePickerController.sourceType = .PhotoLibrary
      imagePickerController.allowsEditing = false

      presentViewController(imagePickerController, animated: true, completion: nil)
   }

   func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
      UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
   }

   func imagePickerControllerDidCancel(picker: UIImagePickerController) {
      self.dismissViewControllerAnimated(true, completion: nil)
   }

   func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
      guard error == nil else {
         //Error saving image
         return
      }
      //Image saved successfully
   }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将completionSelector 和completionTarget 与UIImageWriteToSavedPhotosAlbum 结合使用 的相关文章

随机推荐