PNG图片未写入图库

时间:2018-12-16 23:03:57

标签: ios swift uiimageview

在我的IBACTION上使用以下代码将我的UIImageview> UIImage> PNG放入照片中

但是,当我按IBACTION时,图像不在我的画廊中

    UIGraphicsBeginImageContextWithOptions(self.theView.bounds.size, view.isOpaque, 0)
    theView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let theCapturedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    let data = theCapturedImage!.pngData()
    let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(NSUUID().uuidString+".dat")
    do {
        try data!.write(to: url!, options: [])
    } catch let e as NSError {
        print("Error! \(e)");
        return

1 个答案:

答案 0 :(得分:0)

而不是将数据写入临时目录,请在do块中尝试以下操作:

let image = UIImage(data: data)
UIImageWriteToSavedPhotosAlbum(data, nil, nil, nil) 

通过在Privacy - Photo Library Usage Description中设置带有适当说明的info.plist键,请记住要保存到图库中所需的权限。