1.添加手勢(shì)
//添加長(zhǎng)按手勢(shì)
func addLongPressGestureRecognizer() {
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressClick))
self.qrcodeImageView.userInteractionEnabled = true
self.qrcodeImageView.addGestureRecognizer(longPress)
}
2.長(zhǎng)按手勢(shì)事件
//長(zhǎng)按手勢(shì)事件
func longPressClick() {
let alert = UIAlertController(title: "請(qǐng)選擇", message: nil, preferredStyle: .ActionSheet)
let action = UIAlertAction(title: "保存到相冊(cè)", style: .Default) { [weak self](_) in
//按著command鍵,同時(shí)點(diǎn)擊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.實(shí)現(xiàn)上述方法调缨,保存圖片
//保存二維碼
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil {
let ac = UIAlertController(title: "保存成功", message: "請(qǐng)打開微信識(shí)別二維碼關(guān)注", 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)
}
}