G20过去了,也刚忙一阵子.今天来一个模仿微信长按识别二维码,,,比较简单.望大家勿喷.还是老规则上代码
#import"ViewController.h"
@interfaceViewController()
{
NSTimer*_timer;
}
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
UIImageView*zarimage = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,200,200)];
zarimage.image= [UIImageimageNamed:@"Football-ball@3x"];
zarimage.userInteractionEnabled=YES;
UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(dealLongPress:)];
[zarimageaddGestureRecognizer:longPress];
[self.viewaddSubview:zarimage];// Do any additional setup after loading the view, typically from a nib.}
#pragma mark->长按识别二维码
-(void)dealLongPress:(UIGestureRecognizer*)gesture{
if(gesture.state==UIGestureRecognizerStateBegan){
_timer.fireDate=[NSDatedistantFuture];
UIImageView*tempImageView=(UIImageView*)gesture.view;
if(tempImageView.image){
//1.初始化扫描仪,设置设别类型和识别质量
CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCodecontext:niloptions:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
//2.扫描获取的特征组
NSArray*features = [detectorfeaturesInImage:[CIImageimageWithCGImage:tempImageView.image.CGImage]];
if(features.count>0) {
CIQRCodeFeature*feature = [featuresobjectAtIndex:0];
NSString*scannedResult = feature.messageString;
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction*ok = [UIAlertActionactionWithTitle:@"发送给朋友"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSLog(@"确定");
}];
UIAlertAction*ok1 = [UIAlertActionactionWithTitle:@"收藏"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSLog(@"确定");
}];
UIAlertAction*ok2 = [UIAlertActionactionWithTitle:@"保存图片"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSLog(@"确定");
}];
UIAlertAction*ok3 = [UIAlertActionactionWithTitle:@"识别图中二维码"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSLog(@"确定");
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:scannedResult]];
}];
UIAlertAction*cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action) {
NSLog(@"取消");
}];
if(![cancelvalueForKey:@"_titleTextColor"]) {
[cancelsetValue:[UIColorredColor]forKey:@"_titleTextColor"];
}
[alertaddAction:ok];//添加按钮
[alertaddAction:ok1];//添加按钮
[alertaddAction:ok2];//添加按钮
[alertaddAction:ok3];//添加按钮
[alertaddAction:cancel];//添加按钮
//以modal的形式
[selfpresentViewController:alertanimated:YEScompletion:^{ }];
}else{
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction*ok = [UIAlertActionactionWithTitle:@"发送给朋友"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSLog(@"确定");
}];
UIAlertAction*ok1 = [UIAlertAction actionWithTitle:@"收藏"style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {
NSLog(@"确定");
}];
UIAlertAction*ok2 = [UIAlertAction actionWithTitle:@"保存图片"style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {
NSLog(@"确定");
}];
UIAlertAction*cancel = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {
NSLog(@"取消");
}];
if([cancel valueForKey:@"titleTextColor"]) {
[cancel setValue:[UIColor redColor]forKey:@"titleTextColor"];
}
[alertaddAction:ok];//添加按钮
[alertaddAction:ok1];//添加按钮
[alert addAction:ok2];//添加按钮
[alert addAction:cancel];//添加按钮
NSLog(@"22222222");
//以modal的形式
[selfpresentViewController:alertanimated:YES completion:^{ }];
}//3.获取扫描结果
}else{
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果"message:@"您还没有生成二维码"delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alertViewshow];
}
}else if(gesture.state==UIGestureRecognizerStateEnded){
_timer.fireDate=[NSDate distantPast];
}}