Swift----01 根据图片URL保存到相册

开发环境Xcode12.4,iOS14.4。
H5页面点击保存图片,H5直接调用我这边写好的swift方法保存
先将图片链接地址转换为data数据,然后转换为image格式,最后调用
UIImageWriteToSavedPhotosAlbum方法保存图片

    func downPic(){
        let urlString: String =  "https://gw.chowtaiseng.com/source/productImage/RF1076-W-F-B-RD-01.jpg/"
        guard let url  = URL(string:urlString) else { return }
        let data = NSData(contentsOf: url)
        let image : UIImage = UIImage.init(data: data! as Data)!
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
       
    }
    @objc func image(image: UIImage,didFinishSavingWithError: NSError?,contextInfo: AnyObject) {
 
    if didFinishSavingWithError != nil {
        SGNormalHud.showMbHud(with: self.view, withText: "保存失败")
        return
    }
        SGNormalHud.showMbHud(with: self.view, withText: "保存成功")
}

UIImageWriteToSavedPhotosAlbum(image, nil, ni, nil)这样调用也是可以的,但是这样写,能保存图片,但是没有保存成功或者失败的提示。

你可能感兴趣的:(Swift----01 根据图片URL保存到相册)