时间:2021-05-20
一、说在前面
iPhone 7 之后系统相机支持HEIF格式的图片,在扫描二维码时,该格式的图片需要转换成JPG格式才能进行扫描成功。
二、思路
用户在打开系统相册选择完一张照片后,要判断该照片是否是HEIF格式,如果是,进行格式转换之后再扫描JPG图片的内容,得到扫描结果。
三、具体实现
因为过程中用到photoKit,因此需要加入该头文件,#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自定义类:(.mm)
#define IOS9orLater ([UIDevice currentDevice].systemVersion.integerValue >= 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 (self.selectedAsset == 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__); self.selectedImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]; if (self.selectedImage == nil) { log4cplus_error("TVUImageManagerLog", "%s:selected image is null",__func__); return; } [self.delegate 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 (fetchResult.count == 0) { log4cplus_error("TVUImageManagerLog", "%s:fetchResult's count is 0",__func__); return; } self.selectedAsset = (PHAsset*)fetchResult.firstObject;}// 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:self.selectedAsset]; [resourceList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { PHAssetResource *resource = obj; NSString *UTI = resource.uniformTypeIdentifier; if ([UTI isEqualToString:@"public.heif"] || [UTI isEqualToString:@"public.heic"]) { isHEIF = YES; *stop = YES; } }]; }else { NSString *UTI = [self.selectedAsset valueForKey:@"uniformTypeIdentifier"]; isHEIF = [UTI isEqualToString:@"public.heif"] || [UTI isEqualToString:@"public.heic"]; } 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:self.selectedAsset 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:ciImage.colorSpace options:@{}]; self.jpgImage = [UIImage imageWithData:jpgData]; [self.delegate letQRCodeVCScanQRCodeImageInAlbum:YES]; }];}@end调用:
#pragma mark UIImagePickerControllerDelegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [self.imageMgr doSomethingAfterSelectedImage:info];}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢?在ios7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如zxin
iOS11中可以直接使用相机扫描二维码了,该怎么扫描二维码呢?下面我们就来看看iOS11扫描二维码的方法。1、首先,打开屏幕上的【相机】2、将二维码放到镜头中3
在智能手机越来越普及的今天,二维码也成为大家不能不知道的,那么二维码是什么?二维码扫描软件怎样用?如何使用二维码下载软件?相信对于二维码是什么和二维码扫描软
二维码是什么?二维码扫描软件怎样用?如何使用二维码下载软件?相信对于二维码是什么和二维码扫描软件怎么用这样的问题,很多人听着都一头雾水。本文专门为大家讲解如
这个二维码框架不仅使用的代码少,而且可以生成带图片的二维码,而且二维码扫描界面也是先做好的,也比较好看。我们所需要做的就是在扫描二维码后得到的数据对其进行操作。