agw*_*n27 2 alert screenshot uiimage ios swift
我正试图想办法让我的iOS应用程序将屏幕截图保存到相机胶卷,然后弹出警告,告诉用户屏幕截图已成功保存.
我能想到的唯一方法是使用某种形式的if/else循环(正如你在下面我的伪代码注释中看到的那样),但是我想不出任何与UIImageWriteToSavedPhotosAlbum
UIKit函数一起使用的语法.我遇到建议,说使用completionSelector和completionTarget苹果的开发网站,但我真的不明白如何使用它们,或者我应该用什么样的具体措辞为completionSelector
,并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)
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*are 16
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
}
}
Run Code Online (Sandbox Code Playgroud)
以下是在SWIFT 3.0中保存相机胶卷中当前视图的屏幕截图的工作示例
@IBAction func saveScreenshot(_ sender: Any) {
//Create the UIImage
UIGraphicsBeginImageContextWithOptions(editorView.frame.size, false, UIScreen.main.scale)
// UIGraphicsBeginImageContext(myTextView.frame.size)
editorView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(image!, self, #selector(TextEditorViewController.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
6890 次 |
最近记录: |