简体   繁体   English

将使用 UIImagePickerController 拍摄的图像以 HEIC 格式保存到照片胶卷中

[英]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.

相关问题 使用UIImagePickerController拍摄的照片或图像的本地地址 - Local address of the photo or image taken with UIImagePickerController 将拍摄的图像保存在照片库中 - Save taken image in photo library 如何将从UIImagePickerController中获取的图像保存为HEIF文件? - How to save image taken from UIImagePickerController as HEIF file? 将图像保存到特定的相册,但限制保存到相机胶卷文件夹中 - Save image to a specific photo album, but restrict to save into camera roll folder 如何保存用UIImagePickerController相机拍摄的照片以稍后在应用程序中显示? - How can I save a photo taken with UIImagePickerController camera to be displayed later in the app? UIImagePickerController自定义叠加层预览拍摄的照片 - UIImagePickerController custom overlay preview taken photo 如何保存拍摄的照片 - How to save taken photo 异步加载从UIImagePickerController相机拍摄的照片 - Loading a photo taken from UIImagePickerController camera asynchronously 将UIImagePickerController与源类型camera一起使用时,是否必须将捕获的图像保存到Camera Roll? - Must I save the captured image to the Camera Roll when using UIImagePickerController with source type camera? UIImagePickerController-将录制的视频保存到相机胶卷错误 - UIImagePickerController - Save Recorded Video to Camera Roll Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM