[英]Save image taken with UIImagePickerController to photo roll in HEIC format
If I save an image to the photo roll with the methods I know ( UIImageWriteToSavedPhotosAlbum()
and [PHAssetChangeRequest creationRequestForAssetFromImage:img]
) a subsequent choosing of the saved photo with the UIImagePickerController
gives me a .jpg
image.如果我使用我知道的方法( UIImageWriteToSavedPhotosAlbum()
和[PHAssetChangeRequest creationRequestForAssetFromImage:img]
)将图像保存到照片胶卷,则随后使用UIImagePickerController
选择保存的照片会给我一个.jpg
图像。
info[UIImagePickerControllerReferenceURL]
gives assets-library://asset/asset.JPG?id=B8B231DC-3A84-4F65-AD5E-D6C431CB5F8B&ext=JPG
info[UIImagePickerControllerReferenceURL]
给出assets-library://asset/asset.JPG?id=B8B231DC-3A84-4F65-AD5E-D6C431CB5F8B&ext=JPG
and和
[((PHAsset*)info[UIImagePickerControllerPHAsset]) valueForKey:@"filename"]
gives me @"IMG_5512.JPG"
for example. [((PHAsset*)info[UIImagePickerControllerPHAsset]) valueForKey:@"filename"]
给我@"IMG_5512.JPG"
。
However if I shoot a photo with the "Camera" app, that photo chosen with UIImagePickerController
has the HEIC extension (given that the "High Efficiency" setting is effective in the "Camera" settings).但是,如果我使用“相机”应用拍摄照片,则使用UIImagePickerController
选择的照片具有 HEIC 扩展名(假设“高效”设置在“相机”设置中有效)。
So what is a convenient way to save in HEIC format to the photo roll in response to - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
?那么什么是一种以 HEIC 格式保存到照片胶卷以响应- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
的便捷方法?
Thanks to Matt comments I was able to solve the issue.感谢马特的评论,我能够解决这个问题。 There is a very similar question at How to save image taken from UIImagePickerController as HEIF file?在如何将从 UIImagePickerController 拍摄的图像保存为 HEIF 文件中有一个非常相似的问题? , but not exactly the same. ,但不完全一样。
UIImage* img=(UIImage*)info[UIImagePickerControllerOriginalImage];
NSDictionary* meta=(NSDictionary*)info[UIImagePickerControllerMediaMetadata];
CIContext* ctx=[CIContext context];
CIImage* ci=[[CIImage alloc] initWithImage:img options:@{kCIImageProperties:meta}];
NSData* heicData=[ctx HEIFRepresentationOfImage:ci format:kCIFormatRGBA8 colorSpace:ctx.workingColorSpace options:@{}];
NSString* __block newId=nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
[request addResourceWithType:PHAssetResourceTypePhoto data:heicData options:nil];
newId = request.placeholderForCreatedAsset.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
PHFetchResult* fr=[PHAsset fetchAssetsWithLocalIdentifiers:@[newId] options:nil];
PHAsset* phass=fr.firstObject;
NSLog(@"PHAsset:%@",phass);
} else {
NSLog(@"error:%@",error);
}
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.