iOS从系统相册中识别二维码

UIImagePickerControllerEditedImage]; if (!image) { image = info[UIImagePickerControllerOriginalImage]; } //初始化 将类型设置为二维码 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:nil]; [picker dismissViewControllerAnimated:YES completion:^{ //设置数组,放置识别完之后的数据 NSArray *features = [detector featuresInImage:[CIImage imageWithData:UIImagePNGRepresentation(image)]]; //判断是否有数据(即是否是二维码) if (features.count >= 1) { //取第一个元素就是二维码所存放的文本信息 CIQRCodeFeature *feature = features[0]; NSString *scannedResult = feature.messageString; //通过对话框的形式呈现 [self alertControllerMessage:scannedResult]; }else{ [self alertControllerMessage:@"这不是一个二维码"]; } }];}//由于要写两次,所以就封装了一个方法-(void)alertControllerMessage:(NSString *)message{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil];}