iOS如何扫描HEIF格式的二维码图片
一、说在前面
iPhone7之后系统相机支持HEIF格式的图片,在扫描二维码时,该格式的图片需要转换成JPG格式才能进行扫描成功。
二、思路
用户在打开系统相册选择完一张照片后,要判断该照片是否是HEIF格式,如果是,进行格式转换之后再扫描JPG图片的内容,得到扫描结果。
三、具体实现
因为过程中用到photoKit,因此需要加入该头文件,#import
@protocolTVUImageManagerDelegate-(void)letQRCodeVCScanQRCodeImageInAlbum:(BOOL)isHEIFFormat; @end @interfaceTVUImageManager:NSObject @property(strong,nonatomic)UIImage*jpgImage; @property(strong,nonatomic)UIImage*selectedImage; @property(strong,nonatomic)PHAsset*selectedAsset; @property(nonatomic,strong)id delegate; +(TVUImageManager*)defaultManager; -(void)doSomethingAfterSelectedImage:(NSDictionary*)info; @end
自定义类:(.mm)
#defineIOS9orLater([UIDevicecurrentDevice].systemVersion.integerValue>=9) @implementationTVUImageManager #pragmamark-initpart +(TVUImageManager*)defaultManager { staticTVUImageManager*m_manager=nil; if(m_manager==nil){ m_manager=[[TVUImageManageralloc]init]; } returnm_manager; } -(id)init { self=[superinit]; if(self){ } returnself; } #pragmamark-imageformatpart -(void)doSomethingAfterSelectedImage:(NSDictionary*)info { [selfgetSelectedImagePHAsset:info]; if(self.selectedAsset==nil){ log4cplus_error("TVUImageManagerLog","%s:selectedimageisnull",__func__); return; }else{ BOOLisHEIF=[selfjudgeIfAnImageIsHeifFormat]; if(isHEIF){ log4cplus_error("TVUImageManagerLog","%s:selectedimageisHEIFformat",__func__); [selfconvertImageFormatFromHeifToJpegAndThenScanTheImage]; }else{ log4cplus_error("TVUImageManagerLog","%s:selectedimageisnotHEIFformat",__func__); self.selectedImage=(UIImage*)[infoobjectForKey:UIImagePickerControllerOriginalImage]; if(self.selectedImage==nil){ log4cplus_error("TVUImageManagerLog","%s:selectedimageisnull",__func__); return; } [self.delegateletQRCodeVCScanQRCodeImageInAlbum:NO]; } } } //Chooseapicturefromthesystemalbum -(void)getSelectedImagePHAsset:(NSDictionary*)info { log4cplus_error("TVUImageManagerLog","%s:getselectedimagePHAsset",__func__); NSURL*imageAssetUrl=[infoobjectForKey:UIImagePickerControllerReferenceURL]; PHFetchResult*fetchResult=[PHAssetfetchAssetsWithALAssetURLs:[NSArrayarrayWithObject:imageAssetUrl]options:nil]; if(fetchResult.count==0){ log4cplus_error("TVUImageManagerLog","%s:fetchResult'scountis0",__func__); return; } self.selectedAsset=(PHAsset*)fetchResult.firstObject; } //DetermineifthepictureisinHEIFformat -(BOOL)judgeIfAnImageIsHeifFormat { log4cplus_error("TVUImageManagerLog","%s:begintojudgeifanimageisHEIFformat",__func__); __blockBOOLisHEIF=NO; if(IOS9orLater){ NSArray*resourceList=(NSArray*)[PHAssetResourceassetResourcesForAsset:self.selectedAsset]; [resourceListenumerateObjectsUsingBlock:^(id_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop){ PHAssetResource*resource=obj; NSString*UTI=resource.uniformTypeIdentifier; if([UTIisEqualToString:@"public.heif"]||[UTIisEqualToString:@"public.heic"]){ isHEIF=YES; *stop=YES; } }]; }else{ NSString*UTI=[self.selectedAssetvalueForKey:@"uniformTypeIdentifier"]; isHEIF=[UTIisEqualToString:@"public.heif"]||[UTIisEqualToString:@"public.heic"]; } returnisHEIF; } //ConvertimageformatfromHEIFtoJPEGandthenscantheimage -(void)convertImageFormatFromHeifToJpegAndThenScanTheImage { log4cplus_error("TVUImageManagerLog","%s:begintoconvertimageformatfromHEIFtoJPEG",__func__); [[PHImageManagerdefaultManager]requestImageDataForAsset:self.selectedAssetoptions:nilresultHandler:^(NSData*_NullableimageData,NSString*_NullabledataUTI,UIImageOrientationorientation,NSDictionary*_Nullableinfo){ CIImage*ciImage=[CIImageimageWithData:imageData]; CIContext*context=[CIContextcontext]; NSData*jpgData=[contextJPEGRepresentationOfImage:ciImagecolorSpace:ciImage.colorSpaceoptions:@{}]; self.jpgImage=[UIImageimageWithData:jpgData]; [self.delegateletQRCodeVCScanQRCodeImageInAlbum:YES]; }]; } @end
调用:
#pragmamarkUIImagePickerControllerDelegate -(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info { [self.imageMgrdoSomethingAfterSelectedImage:info]; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。