中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS如何掃描HEIF格式的二維碼圖片

發布時間:2020-10-24 20:24:35 來源:腳本之家 閱讀:266 作者:JennyG_ 欄目:移動開發

一、說在前面

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

溧阳市| 忻城县| 德阳市| 海原县| 红原县| 社旗县| 阿图什市| 吉木萨尔县| 龙江县| 吐鲁番市| 文登市| 苏尼特右旗| 深州市| 九江市| 兴文县| 海安县| 丽水市| 宣汉县| 隆德县| 塔河县| 南靖县| 九龙坡区| 高尔夫| 正蓝旗| 鄂尔多斯市| 无棣县| 金堂县| 郁南县| 东至县| 龙井市| 全南县| 洮南市| 循化| 峨边| 开平市| 铁岭市| 广平县| 铜梁县| 富蕴县| 北宁市| 株洲县|