How to get all PHAssets from MacOS's Photo Library

*爱你&永不变心* 提交于 2019-12-10 10:16:43

问题


I want to parse all images from my MacOS Photos Library, using PHAsset. However all examples I find are only working for iOS, like the one below, where the last line, which retrieves the assets is not available on macos framework:

    NSMutableArray *photos = [[NSMutableArray alloc] init];
    PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

 // Below compilation error as fetchAssetsWithMediaType is only on iOS framework
    PHFetchResult *allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];

What fetchAssets command should I use on MacOS (version 10.13), or is there another way to do so ?


回答1:


The Photos framework — or at least, the main subset of that framework, which provides apps with read/write access to the entire Photos library — is not available in macOS.

Photos and PhotosUI are present in macOS, but only in support of specific app-extension features:

  • Photo Editing extensions. The user invokes these from the Photos app while in Edit mode for a single asset, and it provides direct access to the photo/video data for that asset, so it doesn’t even use the PHAsset class.
  • Photo Project extensions. These involve collections of assets and selectively loading data for each, so more of the PHAsset/PHImageManager system is present. However, it’s still a reduced subset compared to iOS (only what’s needed for a project extension, like fetching the assets in that project), and it’s accessible only to app extensions, not standalone apps.

If you’d like the full Photos framework on macOS, your best bet is to file a feature request with Apple.




回答2:


As of macOS 10.15 Catalina, the Photos and PhotosUI frameworks are now available on the Mac, for both Catalyst and AppKit apps. So you should be able to transfer almost all iOS code that uses PhotoKit over to your Mac app now, as long as it has 10.15 as a minimum system requirement.



来源:https://stackoverflow.com/questions/47652944/how-to-get-all-phassets-from-macoss-photo-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!