从相册获取二维码

iOS8之后,可以使用CIDetector(CIDetector可用于人脸识别)进行图片解析,从而使我们可以便捷的从相册中获取到二维码。

1.调用系统相册,从系统相册中选取图片

2.使用探测器(CIDetector)对选取的图片进行处理,取得图片二维码中包含的数据信息。

下面是简单的代码实现示例

- (void)choicePhoto{

//调用相册

UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

imagePicker.delegate = self;

[self presentViewController:imagePicker animated:YES completion:nil];

}

//选中图片的回调

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

NSString *content = @"" ;

//取出选中的图片

UIImage *pickImage = info[UIImagePickerControllerOriginalImage];

NSData *imageData = UIImagePNGRepresentation(pickImage);

CIImage *ciImage = [CIImage imageWithData:imageData];

//创建探测器

CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];

NSArray *feature = [detector featuresInImage:ciImage];

//取出探测到的数据

for (CIQRCodeFeature *result in feature) {

content = result.messageString;

}//进行处理(音效、网址分析、页面跳转等)

}

你可能感兴趣的:(从相册获取二维码)