iOS HEIF/HEVC 格式
iOS 11更新后,iPhone 7及以上设备,在拍摄照片时的默认图像存储格式是 .heic
。.heic
格式相比普通的jpg、png格式来说,占用手机的内存更小,而且拍摄下来的照片无损度也非常高,这也就是为什么iOS 11会采用这种文件的原因。如下图:
修改相机格式操作截图:
详见:
1、关于 iOS 11 更新
https://support.apple.com/zh-cn/HT208067#11
2、在 Apple 设备上使用 HEIF 或 HEVC 媒体
https://support.apple.com/zh-cn/HT207022
遇到的问题
APP在图片选择的时候,无法显示图片,需要 heic
格式图片转 jpg
才能显示
插件:cordova-plugin-mediapicker-dmcsdk
1、识别HEIF
__block BOOL isHEIF = NO;
if (iOSVersionGreaterThanOrEqualTo(@"9.0")) {
NSArray *resourceList = [PHAssetResource assetResourcesForAsset:phAsset];
[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 = [phAsset valueForKey:@"uniformTypeIdentifier"];
isHEIF = [UTI isEqualToString:@"public.heif"] || [UTI isEqualToString:@"public.heic"];
}
2、转换
[[PHImageManager defaultManager] requestImageDataForAsset:phAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
if (isHEIF) {
CIImage *ciImage = [CIImage imageWithData:imageData];
CIContext *context = [CIContext context];
NSData *jpgData = [context JPEGRepresentationOfImage:ciImage colorSpace:ciImage.colorSpace options:@{}];
} else {
}
}];
3、修改插件源码
#define iOSVersionGreaterThanOrEqualTo(v)([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch]!=NSOrderedAscending)
...
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
NSString *filename=[asset valueForKey:@"filename"];
NSString *fullpath=[NSString stringWithFormat:@"%@/%@%@", dmcPickerPath,[[NSProcessInfo processInfo] globallyUniqueString], filename];
//增加 判断如果heic图片 转换一次
__block BOOL isHEIF = NO;
if (iOSVersionGreaterThanOrEqualTo(@"9.0")) {
NSArray *resourceList = [PHAssetResource assetResourcesForAsset:asset];
[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 = [asset valueForKey:@"uniformTypeIdentifier"];
isHEIF = [UTI isEqualToString:@"public.heif"] || [UTI isEqualToString:@"public.heic"];
}
if (isHEIF) {
CIImage *ciImage = [CIImage imageWithData:imageData];
CIContext *context = [CIContext context];
imageData = [context JPEGRepresentationOfImage:ciImage colorSpace:ciImage.colorSpace options:@{}];
}
//增加 判断如果heic图片 转换一次 end
//对图片进行压缩
UIImage *photo = [UIImage imageWithData: imageData];
NSData *data;
if ((limit_width > 0 || limit_height > 0) && limit_quality > 0) {
float q = (float)limit_quality/100;
CGFloat imgWidth = photo.size.width;
CGFloat imgHeight = photo.size.height;
//limit_width设置为0 不限宽 最大宽度为图片原宽
int max_width=limit_width==0?imgWidth:limit_width;
//limit_height设置为0 不限高 最大高度为图片原高
int max_height=limit_height==0?imgHeight:limit_height;
data = [UIImage compressScale:photo maxWidth:max_width maxHeight:max_height quality:q];
} else {
data = [UIImage lubanCompressImage:photo];
}
NSString *dmcPickerPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"dmcPicker"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:dmcPickerPath ]){
[fileManager createDirectoryAtPath:dmcPickerPath withIntermediateDirectories:YES attributes:nil error:nil];
}
filename = [NSString stringWithFormat:@"%@%@%@",@"dmcMediaPickerCompress", [self currentTimeStr],@".jpg"];
fullpath = [NSString stringWithFormat:@"%@/%@", dmcPickerPath,filename];
NSNumber* size = [NSNumber numberWithLong: data.length];
NSError *error = nil;
if (![data writeToFile:fullpath options:NSAtomicWrite error:&error]) {
NSLog(@"%@", [error localizedDescription]);
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]] callbackId:callbackId];
} else {
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:fullpath,@"path",[[NSURL fileURLWithPath:fullpath] absoluteString],@"uri",@"image",@"mediaType",size,@"size",[NSNumber numberWithInt:index],@"index", nil];
[aListArray addObject:dict];
if([aListArray count]==[selectArray count]){
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:aListArray] callbackId:callbackId];
}
}
}];
网友评论