首页 > 解决方案 > PNG图像未写入画廊

问题描述

在我的 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

标签: iosswiftuiimageview

解决方案


不要将数据写入临时目录,而是在do块内尝试:

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

请记住,通过Privacy - Photo Library Usage Descriptioninfo.plist.


推荐阅读