1.格式转换方法一
PHAsset * asset = [_assetDic valueForKey:_articleData.img];//PHAsset已经事先存到字典中
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = PHImageRequestOptionsResizeModeExact;
option.networkAccessAllowed = YES;
option.synchronous = YES;//同步执行
// 从asset中获得图片
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
NSLog(@"---格式-- > %@",dataUTI);
NSData *imageDataW = nil;
if ([dataUTI isEqualToString:@"public.heif"] || [dataUTI isEqualToString:@"public.heic"]) {
CIImage *ciImage = [CIImage imageWithData:imageData];
CIContext *context = [CIContext context];
NSData *jpgData = [context JPEGRepresentationOfImage:ciImage colorSpace:ciImage.colorSpace options:@{}];
imageDataW = jpgData;
} else {
imageDataW = imageData;
}
if ([imageDataW length] / (1024 * 1024) > 3.0) {
imageDataW = [[UIImage imageWithData: imageDataW]compressAndResize]; //图片大于3M压缩操作,可不调用此方法
}
// self.orignalImage = [CIImage imageWithCGImage:[[UIImage imageWithData: imageDataW] CGImage]];
//图片编辑用到的CIImage
imgView.image = [UIImage imageWithData: imageDataW];
}];
2.照片格式转换方法二__block BOOL isHEIForHEIC = NO;
PHAsset * asset = [_assetDic valueForKey:_articleData.img];//PHAsset已经事先存到字典中
NSArray *assetResourceArr = [PHAssetResource assetResourcesForAsset:asset];
[assetResourceArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAssetResource *resource = obj;
NSString *extension = resource.uniformTypeIdentifier;
NSLog(@"------图片格式----> %@",extension);
if ([extension isEqualToString:@"public.heif"] || [extension isEqualToString:@"public.heic"]) {
isHEIForHEIC = YES;
*stop = YES;
}
}];
//用isHEIForHEIC代替方法一中的dataUTI来判断图片格式,调用。方法一
3.图片裁剪
CGRect rectimg = CGRectMake(0,0,200,300);//图片裁剪范围
// NSLog(@"=== > %@",NSStringFromCGRect(rectimg));
CGImageRef imageRef=CGImageCreateWithImageInRect([image CGImage],rectimg);
UIImage *image1=[UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);//释放CGImageRef,否则内存升高
_faceIageView.image = image1;