【问题标题】:PHAsset get original file namePHAsset 获取原始文件名
【发布时间】:2015-12-17 16:36:03
【问题描述】:

我想知道是否有任何方法可以使用 PHAsset 获取原始文件名?

我使用以下代码提取文件信息。

   [[PHImageManager defaultManager] requestImageDataForAsset:asset options:requestOption resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
    entity.fileUrl =  [info objectForKey:@"PHImageFileURLKey"];
    entity.filename = [[NSFileManager defaultManager] displayNameAtPath:[ entity.fileUrl path]];
   }];

但是,它不返回原始名称,而是返回格式为“img_123”的名称 我刚刚检查了苹果官方文档。引入了一个新类PHAssetResource 和属性originalFilename,在iOS 9+ 中可用。问题是我使用基于照片框架的图像选择器库CTAssetsPickerController;它将拾取的图像作为 PHAsset 对象返回。 PS。我正在寻找与 iOS 8 兼容的解决方案 :)。 谢谢!

【问题讨论】:

    标签: ios objective-c ios8 phasset


    【解决方案1】:

    在 iOS 8 上,您的解决方案是获取文件名的正确方法(也是唯一方法)。

    在 iOS 9 上可以使用:

    NSArray *resources = [PHAssetResource assetResourcesForAsset:asset];
    NSString *orgFilename = ((PHAssetResource*)resources[0]).originalFilename;
    

    【讨论】:

    • ios 8 或以前的版本应该怎么做?
    • 自 iOS 13 以来这是错误的。您可能会得到 Adjustments.plist 或实时照片电影(用于照片)。
    【解决方案2】:

    用一行代码获取文件名的快捷方式。资产具有访问文件名的属性。

     NSString*FileName=[asset valueForKey:@"filename"];
     NSLog(@"File name %@",FileName);
    
    Its done.
    
    Note: Accepted answer takes lots of time to load a phasset but it works.
    

    【讨论】:

    • 这给出的结果与接受的结果不同。是内名,不是原名
    【解决方案3】:

    我不得不修改我的代码,因为它开始返回无意义的名称。我的解决方案是根据资产的 mediaType 和资源的类型选择资源,但也许有更简单的方法:

    extension PHAsset {
        var primaryResource: PHAssetResource? {
            let types: Set<PHAssetResourceType>
    
            switch mediaType {
            case .video:
                types = [.video, .fullSizeVideo]
            case .image:
                types = [.photo, .fullSizePhoto]
            case .audio:
                types = [.audio]
            case .unknown:
                types = []
            @unknown default:
                types = []
            }
    
            let resources = PHAssetResource.assetResources(for: self)
            let resource = resources.first { types.contains($0.type)}
    
            return resource ?? resources.first
        }
    
        var originalFilename: String {
            guard let result = primaryResource else {
                return "file"
            }
    
            return result.originalFilename
        }
    }
    

    【讨论】:

      【解决方案4】:

      也许你可以使用该方法,它适用于iOS8以上:

       [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
      
          CIImage *fullImage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
          NSLog(@"%@",contentEditingInput.fullSizeImageURL);//get url
          NSLog(@"%@", fullImage.properties.description);//get {TIFF}, {Exif}
      }];
      

      【讨论】:

      • 嘿,谢谢。我使用了接受的答案中建议的方法。但是在挖掘源(头文件)之后,我发现我可以使用类似于你的方法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 2011-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多