iOS swift-将图片保存到相册

来源:互联网 发布:网络拓扑工具 编辑:程序博客网 时间:2024/04/29 15:48

目的 : 调用系统函数,将显示在UIImageView上的图片保存到相册中

public func UIImageWriteToSavedPhotosAlbum(image: UIImage, _ completionTarget: AnyObject?, _ completionSelector: Selector, _ contextInfo: UnsafeMutablePointer<Void>)

_ completionSelector 所需要传入的函数不是随便乱写的,在系统函数上方已经给出了示例函数

//  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
它会自动给我们返回三个值

接着我们需要将这个转为swift语句


直接上示例代码:

// 将image对象保存到相册中UIImageWriteToSavedPhotosAlbum(image, self, #selector(PhotoBrowserController.image(_:didFinishSavingWithError:contextInfo:)), nil)// Selector的旧写法// UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
监听方法
// - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;@objc private func image(image : UIImage, didFinishSavingWithError error : NSError?, contextInfo : AnyObject) {    var showInfo = ""    if error != nil {        showInfo = "保存失败"    } else {        showInfo = "保存成功"    }        SVProgressHUD.showInfoWithStatus(showInfo)}


0 0
原创粉丝点击