一、说在前面

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];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • iOS和Android用同一个二维码实现跳转下载链接的方法
  • IOS生成与读取二维码名片
  • iOS实现二维码的扫描功能
  • IOS笔记061之二维码的生成和扫描
  • ios原生二维码扫描
  • ios swift3.0实现二维码扫描、生成、识别示例代码
  • iOS 条码及二维码扫描(从相册中读取条形码/二维码)及扫码过程中遇到的坑
  • iOS模仿微信长按识别二维码的多种方式
  • iOS自带原生二维码扫描的实现
  • 如何在iphon IOS设备上使用二维码
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。