0

我试图找出一种方法让我的 iOS 应用程序将屏幕截图保存到相机胶卷,然后弹出警报告诉用户屏幕截图已成功保存。我能想到的唯一方法是使用某种形式的 if/else 循环(正如您将在下面的伪代码注释中看到的那样),但我想不出任何语法可以与 UIKit 中的 UIImageWriteToSavedPhotosAlbum 函数一起使用. 有什么建议么?

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)
}
4

1 回答 1

0

参考文档中,您可以看到有 acompletionTarget和 a completionSelector,这是某种基本委托(因为它没有强制执行您的类必须遵守的某种协议,以用作委托目标)。例如,您将在您的类中实现一个函数,该函数符合由定义的指定签名completionSelector,然后传递selfascompletionTarget并将函数的名称传递为completionSelector

在您的情况下,这将是:

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

    UIImageWriteToSavedPhotosAlbum(image, self, "didSaveScreenshot:", nil)
}

func didSaveScreenshot(image: UIImage?, didFinishSavingWithError error: NSError?, contextInfo contextInfo: AnyObject?) 
{
    if let error = error {
        //Not successful
    } else {
        //Success
    }
}

注意:我没有对此进行测试,但它应该可以工作。唯一的问题是,我不确定如何将 void 指针从 Objective-C 移植到 Swift,所以大家可以在这里纠正我。

于 2015-03-31T21:00:49.993 回答