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)
}
}
网友评论