Painted painted levels: fostered fostered fostered
Tags: iOS CIDetector CIQRCodeFeature
The author:Xs h.
Review:QiShare team
Following the previous iOS scanning qr code/bar code, this paper introduces the implementation of scanning the QR code on the photo album. Take a look at an example effect from QiQRCode:
After iOS 8, the CIQRCodeFeature of CIDetector can be used to scan the QR code on the photo album. The specific implementation process is as follows:
1. Abide by the agreement
@interface QiCodeManager () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Copy the code
2. Open the album
- (void)presentPhotoLibraryWithRooter:(UIViewController *)rooter callback:(nonnull void (^)(NSString * _Nonnull))callback {
_callback = callback;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
[rooter presentViewController:imagePicker animated:YES completion:nil];
}
Copy the code
3. Implement proxy
// UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *pickedImage = info[UIImagePickerControllerEditedImage] ? : info[UIImagePickerControllerOriginalImage]; CIImage *detectImage = [CIImage imageWithData:UIImagePNGRepresentation(pickedImage)]; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}]; CIQRCodeFeature *feature = (CIQRCodeFeature *)[detector featuresInImage:detectImage options:nil].firstObject; [picker dismissViewControllerAnimated:YES completion:^{ if (feature.messageString) { [self handleCodeString:feature.messageString]; } }]; }Copy the code
4. Transparent transmission service
- (void)handleCodeString:(NSString *)codeString { if (_autoStop) { [self stopScanning]; } if (_callback) { _callback(codeString); }}Copy the code
5. Business invocation
/ / create "album" button UIBarButtonItem * photoItem = [[UIBarButtonItem alloc] initWithTitle: @ "album" style: UIBarButtonItemStylePlain target:self action:@selector(photo:)]; self.navigationItem.rightBarButtonItem = photoItem;Copy the code
- (void)photo:(id)sender {__weak typeof(self) weakSelf = self; [_codeManager presentPhotoLibraryWithRooter:self callback:^(NSString * _Nonnull code) { [weakSelf performSegueWithIdentifier:@"showCodeGeneration" sender:code]; }]; }Copy the code
The core step in the code above is step 3 – implement the proxy.
We’re throughUIImagePickerController
getimage
After usingCIDetector
andCIQRCodeFeature
readimage
And finally got itfeature.messageString
Is the string information of the TWO-DIMENSIONAL code.
The sample source code QiQRCode is available from GitHub’s QiShare open source library.
Ways to focus on us are:
QiShare(GitHub) QiShare(CocoaChina) QiShare(StackOverflow) QiShare(wechat)
Recommended articles:
IOS Xcode Bitcode iOS drawRect tO write high quality Objective-C code (8) iOS KVC and KVO introduction to The Dance Weekly