Ios 将UIImage保存到摄影机卷仅在模拟器上工作

Ios 将UIImage保存到摄影机卷仅在模拟器上工作,ios,iphone,uiimageview,uiimage,Ios,Iphone,Uiimageview,Uiimage,我正在尝试将屏幕上的图像保存到相机卷。我使用的方法在模拟器中工作得绝对完美,但在实际设备上,图像是100%白色的,我不知道为什么。这是密码 @IBAction func saveImage(sender: AnyObject) { let snapShot:UIView = backgroundImage.snapshotViewAfterScreenUpdates(true) UIGraphicsBeginImageContext(backgroundImage.bound

我正在尝试将屏幕上的图像保存到相机卷。我使用的方法在模拟器中工作得绝对完美,但在实际设备上,图像是100%白色的,我不知道为什么。这是密码

@IBAction func saveImage(sender: AnyObject) {

    let snapShot:UIView = backgroundImage.snapshotViewAfterScreenUpdates(true)

    UIGraphicsBeginImageContext(backgroundImage.bounds.size)

    snapShot.drawViewHierarchyInRect(backgroundImage.bounds, afterScreenUpdates: true)

    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)


}
同样,图像完美地保存在模拟器上,但实际设备接收到一个空白图像

我知道该怎么办了:

@IBAction func saveImage(sender: AnyObject) {

    UIGraphicsBeginImageContextWithOptions(backgroundImage.bounds.size, true, 1.0)
    backgroundImage.drawViewHierarchyInRect(backgroundImage.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)


}

它应该在真正的设备上工作。对于模拟器,您可以通过长按图像将图像从浏览器保存到库中。@PrabhatPankaj如果它起作用,我就不会问这个问题了。不过我是自己想出来的。