PHAsset get original file name

2019-03-09 21:59发布

问题:

I wonder if there any way to get the original file name using PHAsset?

I use the following code to extract the file info.

   [[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]];
   }];

However, It doesn't return original name but the name in the format "img_123" I've just checked official apple docs . there has been introduced a new class PHAssetResource and the property originalFilename which is available in the iOS 9+. The problem is that I use the image picker library CTAssetsPickerController which 's based on the Photos framework; it returns picked image as the PHAsset object . PS. I'm looking for the solution which is compatible with iOS 8 :). Thank you!

回答1:

On iOS 8 your solution is the right (and only approach) to get a filename at all.

On iOS 9 this works:

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


回答2:

Short way to get file name with one line of code. Asset have a property for accessing file name.

 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:

Maybe you can use the method, it works above 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}
}];