gpt4 book ai didi

ios - 将 completionSelector 和 completionTarget 与 UIImageWriteToSavedPhotosAlbum 一起使用

转载 作者:可可西里 更新时间:2023-10-31 23:51:49 26 4
gpt4 key购买 nike

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

我能想到的唯一方法是使用某种形式的 if/else 循环(正如您将在下面的伪代码注释中看到的那样),但我想不出任何语法来处理 UIImageWriteToSavedPhotosAlbum来自 UIKit 的函数。我在 Apple 的开发网站上遇到了使用 completionSelector 和 completionTarget 的建议,但我真的不明白如何使用它们或者我应该在我的代码中对 completionSelectorcompletionTarget 使用什么具体的措辞。我对 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
}

}

关于ios - 将 completionSelector 和 completionTarget 与 UIImageWriteToSavedPhotosAlbum 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29501502/

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