Im trying to get the image name using PHAssets
. But I couldn't find metadata for filename or any method to get the image name. Is there a different way to get the file name?
In iPhoto, when "Titles" was selected under the View menu, the camera-assigned filenames would appear below the photos. You could then edit the "titles" as seen below: In Photos, these "Titles" do not appear, even if View -> Metadata -> Titles is selected. The only way to see them is to in the "Info" panel, as you mentioned above.
(The title field is metadata stored within the photo file itself, while the filename is stored in the file system.) iPhoto displays the file name (less its extension) if the title metadata field is empty & supports searching on it; Photos does neither.
In Photos, these "Titles" do not appear, even if View -> Metadata -> Titles is selected. The only way to see them is to in the "Info" panel, as you mentioned above. This makes the hours and hours of time I have spent over the years renaming photos' filenames to a more meaningful "title" in iPhoto totally wasted!
So what you need is a practical way to add the filename to the title field of the photo if you are to continue using your organizational scheme in the Photos app. This should be doable using Applescript with either iPhoto (before importing the photos into Photos.app) or afterwards with the Applescript support built into Photos.app.
I know the question has already been answered, but I figured I would provide another option:
extension PHAsset { var originalFilename: String? { var fileName: String? if #available(iOS 9.0, *) { let resources = PHAssetResource.assetResources(for: self) if let resource = resources.first { fileName = resource.originalFilename } } if fileName == nil { /// This is an undocumented workaround that works as of iOS 9.1 fileName = self.value(forKey: "filename") as? String } return fileName } }
If you want to get the image name (for example name of last photo in Photos) like IMG_XXX.JPG, you can try this:
PHAsset *asset = nil; PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions]; if (fetchResult != nil && fetchResult.count > 0) { // get last photo from Photos asset = [fetchResult lastObject]; } if (asset) { // get photo info from this asset PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init]; imageRequestOptions.synchronous = YES; [[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { NSLog(@"info = %@", info); if ([info objectForKey:@"PHImageFileURLKey"]) { // path looks like this - // file:///var/mobile/Media/DCIM/###APPLE/IMG_####.JPG NSURL *path = [info objectForKey:@"PHImageFileURLKey"]; } }]; }
Hope it helps.
In Swift the code will look like this
PHImageManager.defaultManager().requestImageDataForAsset(asset, options: PHImageRequestOptions(), resultHandler: { (imagedata, dataUTI, orientation, info) in if info!.keys.contains(NSString(string: "PHImageFileURLKey")) { let path = info![NSString(string: "PHImageFileURLKey")] as! NSURL } })
Swift 4:
let fetchResult = PHAsset.fetchAssets(with: .image, options: nil) if fetchResult.count > 0 { if let asset = fetchResult.firstObject { let date = asset.creationDate ?? Date() print("Creation date: \(date)") PHImageManager.default().requestImageData(for: asset, options: PHImageRequestOptions(), resultHandler: { (imagedata, dataUTI, orientation, info) in if let info = info { if info.keys.contains(NSString(string: "PHImageFileURLKey")) { if let path = info[NSString(string: "PHImageFileURLKey")] as? NSURL { print(path) } } } }) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With