【问题标题】:Get base64 photo from iOS photo library uri从 iOS 照片库 uri 中获取 base64 照片
【发布时间】:2017-05-14 19:38:01
【问题描述】:

我正在使用 React Native 构建一个应用程序,我需要使用照片 uri(例如 photos://A2B9B28B-9A3E-4190-BCA0-B11F1E587085/L0/002)从照片库中以 base64 格式获取图像。我最初使用的是资产库,但我更新了我的应用以使用新的照片库,但我正在努力让它工作。

我的旧资产库方法是:

{
  NSURL *url = [[NSURL alloc] initWithString:input];
  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  [library assetForURL:url resultBlock:^(ALAsset *asset) {
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef imageRef = [rep fullScreenImage];
    NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
    NSString *base64Encoded = [imageData base64EncodedStringWithOptions:0];
    callback(@[[NSNull null], base64Encoded]);
  } failureBlock:^(NSError *error) {
    NSLog(@"that didn't work %@", error);
    callback(@[error]);
  }];
}

【问题讨论】:

  • 获取图片后可以转base 64
  • 只需要解决photos://A2B9B28B-9A3E-4190-BCA0-B11F1E587085/L0/002NSData的转换即可; base64 的东西无关紧要且令人困惑
  • 你是如何获得photos://A2B9B28B-9A3E-4190-BCA0-B11F1E587085/L0/002的?
  • 来自github.com/olofd/react-native-photos-framework。正如我在帖子中所说,我使用的是 React Native,所以目标是将 Javascript 中的图像作为 base64;所以我不确定你是怎么觉得它不相关或令人困惑的。

标签: ios objective-c react-native


【解决方案1】:

它与您已有的代码基本相同,只是分为两部分。首先使用 id 获取 PHAsset,然后使用 PHImageManager 从该资产中获取图像。

  • 要从库中获取PHAsset,请使用

[+ (PHFetchResult<PHAsset *> *)fetchAssetsWithALAssetURLs:(NSArray<NSURL *> *)assetURLs options:(nullable PHFetchOptions *)options;

此方法是同步的,并立即返回PHAsset 的数组,但这些资产不包含任何图像数据。

  • 下次使用

[PHImageManager defaultManager] 获取图像。这与 ALAssetsLibrary 方法一样是异步的。使用- (PHImageRequestID)requestImageDataForAsset:(PHAsset *)asset options:(PHImageRequestOptions *)options resultHandler:(void (^)(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info))resultHandler;

代码应该是这样的(显然您必须为您的应用程序填写错误代码和错误域):

PHFetchResult* results = [PHAsset fetchAssetsWithLocalIdentifiers:@[input] options:nil];
PHAsset *asset = [results firstObject];
if (asset) {
    [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
        if (imageData){
            NSString *base64Encoded = [imageData base64EncodedStringWithOptions:0];
            callback(@[[NSNull null], base64Encoded]);
        }else{
            NSError* error = info[PHImageErrorKey];
            if (!error) {
                error = [NSError errorWithDomain:@"" code:-1 userInfo:@{NSLocalizedDescriptionKey:@"image not found"}];
            }
            callback(nil, error);

        }
    }];
}else{
    NSError* error = [NSError errorWithDomain:@"" code:-1 userInfo:@{NSLocalizedDescriptionKey:@"asset not found"}];
    callback(nil, error);
}

【讨论】:

    【解决方案2】:

    可以在PHAsset+ (PHFetchResult<PHAsset *> *)fetchAssetsWithALAssetURLs:(NSArray<NSURL *> *)assetURLs options:(nullable PHFetchOptions *)options;看到api

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多