Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 在Swift中使用UIKit函数的If/Else语句_Xcode_Swift_Uikit_Alert_Camera Roll - Fatal编程技术网

Xcode 在Swift中使用UIKit函数的If/Else语句

Xcode 在Swift中使用UIKit函数的If/Else语句,xcode,swift,uikit,alert,camera-roll,Xcode,Swift,Uikit,Alert,Camera Roll,我正试图找到一种方法,让我的iOS应用程序将屏幕截图保存到摄像机上,然后弹出一个警报,告诉用户屏幕截图已成功保存。我认为实现这一点的唯一方法是使用某种形式的if/else循环,正如您在下面的伪代码注释中所看到的,但是我想不出任何语法来使用UIKit中的UIImageWriteToSavedPhotosAlbum函数。有什么建议吗 func screenshotMethod() { UIGraphicsBeginImageContextWithOptions(HighchartsView.

我正试图找到一种方法,让我的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)
}
从中可以看到,有一个completionTarget和一个completionSelector,这是一种基本的委托,因为它没有强制实现类必须遵守的某种协议,以用作委托目标。例如,您可以在类中实现一个函数,该函数符合completionSelector定义的指定签名,然后将self作为completionTarget传递,将函数名作为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
    }
}

注意:我没有测试这个,但它应该可以工作。唯一的问题是,我不知道如何将无效指针从Objective-C移植到Swift,所以大家可以在这里随意更正。

您的问题误导了您对问题的描述。所以,您想知道如何知道UIImageWriteToSavedPhotosAlbum是否成功保存了图像?最好的方法是使用selector参数,从所选选择器中获取N错误并检查它。答案对如何在Objective-C中实现这一点有很好的指导意义,Objective-C很容易适应Swift。