1. Say it ahead
After iPhone 7, the system camera supports HEIF format pictures. When scanning the QR code, the pictures in this format need to be converted into JPG format before they can be successfully scanned.
2. Ideas
After the user opens the system album and selects a photo, he needs to determine whether the photo is in HEIF format. If so, scan the content of the JPG picture after format conversion to get the scan result.
3. Specific implementation
Because photoKit is used in the process, the header file needs to be added, #import
@protocol TVUImageManagerDelegate <NSObject> - (void)letQRCodeVCScanQRCodeImageInAlbum:(BOOL)isHEIFFormat; @end @interface TVUImageManager : NSObject @property (strong, nonatomic) UIImage *jpgImage; @property (strong, nonatomic) UIImage *selectedImage; @property (strong, nonatomic) PHAsset *selectedAsset; @property (nonatomic,strong) id <TVUImageManagerDelegate> delegate; + (TVUImageManager *)defaultManager; - (void)doSomethingAfterSelectedImage:(NSDictionary *)info; @end
Custom class: (.mm)
#define IOS9orLater ([UIDevice currentDevice]. >= 9) @implementation TVUImageManager #pragma mark - init part + (TVUImageManager *)defaultManager { static TVUImageManager *m_manager = nil; if (m_manager == nil) { m_manager = [[TVUImageManager alloc] init]; } return m_manager; } - (id)init { self = [super init]; if (self) { } return self; } #pragma mark - image format part - (void)doSomethingAfterSelectedImage:(NSDictionary *)info { [self getSelectedImagePHAsset:info]; if ( == nil) { log4cplus_error("TVUImageManagerLog", "%s:selected image is null",__func__); return; }else { BOOL isHEIF = [self judgeIfAnImageIsHeifFormat]; if (isHEIF) { log4cplus_error("TVUImageManagerLog", "%s:selected image is HEIF format",__func__); [self convertImageFormatFromHeifToJpegAndThenScanTheImage]; }else { log4cplus_error("TVUImageManagerLog", "%s:selected image is not HEIF format",__func__); = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]; if ( == nil) { log4cplus_error("TVUImageManagerLog", "%s:selected image is null",__func__); return; } [ letQRCodeVCScanQRCodeImageInAlbum:NO]; } } } // Choose a picture from the system album - (void)getSelectedImagePHAsset:(NSDictionary *)info { log4cplus_error("TVUImageManagerLog", "%s:get selected image PHAsset",__func__); NSURL *imageAssetUrl = [info objectForKey:UIImagePickerControllerReferenceURL]; PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:imageAssetUrl] options:nil]; if ( == 0) { log4cplus_error("TVUImageManagerLog", "%s:fetchResult's count is 0",__func__); return; } = (PHAsset*); } // Determine if the picture is in HEIF format - (BOOL)judgeIfAnImageIsHeifFormat { log4cplus_error("TVUImageManagerLog", "%s:begin to judge if an image is HEIF format",__func__); __block BOOL isHEIF = NO; if (IOS9orLater) { NSArray *resourceList = (NSArray *)[PHAssetResource assetResourcesForAsset:]; [resourceList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { PHAssetResource *resource = obj; NSString *UTI = ; if ([UTI isEqualToString:@""] || [UTI isEqualToString:@""]) { isHEIF = YES; *stop = YES; } }]; }else { NSString *UTI = [ valueForKey:@"uniformTypeIdentifier"]; isHEIF = [UTI isEqualToString:@""] || [UTI isEqualToString:@""]; } return isHEIF; } // Convert image format from HEIF to JPEG and then scan the image - (void)convertImageFormatFromHeifToJpegAndThenScanTheImage { log4cplus_error("TVUImageManagerLog", "%s:begin to convert image format from HEIF to JPEG",__func__); [[PHImageManager defaultManager] requestImageDataForAsset: options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) { CIImage *ciImage = [CIImage imageWithData:imageData]; CIContext *context = [CIContext context]; NSData *jpgData = [context JPEGRepresentationOfImage:ciImage colorSpace: options:@{}]; = [UIImage imageWithData:jpgData]; [ letQRCodeVCScanQRCodeImageInAlbum:YES]; }]; } @end
Called:
#pragma mark UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [ doSomethingAfterSelectedImage:info]; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.