美文网首页
iOS AVFoundation 从相册中读取二维码

iOS AVFoundation 从相册中读取二维码

作者: 冯龙胜 | 来源:发表于2017-09-26 16:31 被阅读0次

    1、例如在我们的导航栏中设置一个按钮

    -(void)setupRightMenuButton{

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"相册中选"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(takeQRCodeFromPic)];

    }

    2、初始化相册拾取器

    -(void)takeQRCodeFromPic{

    if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){

    UIImagePickerController*controller = [[UIImagePickerControlleralloc] init];

    controller.delegate =self;

    controller.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

    [selfpresentViewController:controller animated:YEScompletion:NULL];

    }else{

    UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"设备不支持访问相册,请在设置->隐私->照片中进行设置!"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

    [alert show];

    }

    }

    3、实现选中照片之后的代理事件,并将图片数据进行转码扫描

    - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info

    {

    //1.获取选择的图片

    UIImage*image = info[UIImagePickerControllerOriginalImage];

    //2.初始化一个监测器CIDetector这个类目前只能识别二维码 不能识别条形码

    CIDetector*detector = [CIDetectordetectorOfType:CIDetectorTypeQRCodecontext:niloptions:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

    [picker dismissViewControllerAnimated:YEScompletion:^{

    //监测到的结果数组

    NSArray*features = [detector featuresInImage:[CIImageimageWithCGImage:image.CGImage]];

    if(features.count >=1) {

    /**结果对象 */

    CIQRCodeFeature*feature = [features objectAtIndex:0];

    NSString*scannedResult = feature.messageString;

    RxWebViewController* webViewController = [[RxWebViewController alloc] initWithUrl:[NSURLURLWithString:scannedResult]];

    webViewController.navigationItem.hidesBackButton =YES;

    [self.navigationController pushViewController:webViewController animated:YES];

    }

    else{

    UIAlertView* alertView = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"该图片没有包含一个二维码!"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

    [alertView show];

    }

    }];

    }

    相关文章

      网友评论

          本文标题:iOS AVFoundation 从相册中读取二维码

          本文链接:https://www.haomeiwen.com/subject/alasextx.html