我怎么知道这段代码是否真的有效?

时间:2015-08-17 22:11:24

标签: ios swift

func screenShotMethod() {
    //Create the UIImage
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    //Save it to the camera roll
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

我是第一次使用应用程序构建器,我在这里找到了这个代码,它构建成功但是当我按下按钮在模拟器上执行它时,它不会将图像发送到模拟器上的相册,但是它不会发回任何错误消息或崩溃。所以我试图找出如何在我正在构建的应用程序中正常运行此代码?

1 个答案:

答案 0 :(得分:-1)

试试这个。你应该添加UIGraphicsBeginImageContextWithOptions

    func screenShotMethod() {

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, 0.0)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }