swift 长按保存图片

1.添加手势

//添加长按手势
func addLongPressGestureRecognizer() {
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressClick))
    self.qrcodeImageView.userInteractionEnabled = true
    self.qrcodeImageView.addGestureRecognizer(longPress)
}

2.长按手势事件

//长按手势事件
func longPressClick() {
    let alert = UIAlertController(title: "请选择", message: nil, preferredStyle: .ActionSheet)
    let action = UIAlertAction(title: "保存到相册", style: .Default) { [weak self](_) in
    //按着command键,同时点击UIImageWriteToSavedPhotosAlbum方法可以看到
    UIImageWriteToSavedPhotosAlbum(self!.qrcodeImageView.image!, self!, #selector(self!.image(_:didFinishSavingWithError:contextInfo:)), nil)
    let cancel = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
    alert.addAction(action)
    alert.addAction(cancel)
    self.presentViewController(alert, animated: true, completion: nil)
}

Adds a photo to the saved photos album. The optional completionSelector should have the form:
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError*)error contextInfo:(void *)contextInfo;

3.实现上述方法,保存图片

//保存二维码
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    if error == nil {
        let ac = UIAlertController(title: "保存成功", message: "请打开微信识别二维码关注", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "好的", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "保存失败", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "好的", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容