如何在 macOS 上的照片应用程序中访问照片



>我正在尝试访问存储在macOS照片应用程序中的照片,并将此代码放在ViewController类中。

import Cocoa
import Photos
class vcSecond: NSViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
    let identifiers = PHPhotoLibrary.localIdentifiers(PHPhotoLibrary())
    let assets = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: fetchOptions)
    print(assets.count)
}
}

我正在努力获取用于获取资产方法的本地标识符。XCode 给我错误"无法将类型'([PHCloudIdentifier]) -> [String]' 的值转换为预期的参数类型'[字符串]'"。有许多代码示例可用于 iOS,但有很多代码示例可用于 macOS。有人愿意分享一些代码和/或提供任何提示吗?谢谢。

好吧,如果您尝试做的只是访问照片,那么要打开照片应用程序,只需将此代码片段添加到您的控制器即可

override func viewDidLoad() {
    super.viewDidLoad()
    NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Photos.app"))
}

另请参阅此线程: 如何从MacOS的照片库中获取所有PHAsset

最新更新