简体   繁体   English

如何在macOS上的``照片''应用中访问照片

[英]How to access photos in Photos app on macOS

I'm trying to access photos stored in macOS Photos app and have this code in ViewController class. 我正在尝试访问存储在macOS Photos应用中的照片,并在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)
}
}

and I'm struggled with getting of localIdentifiers for fetchAssets method. 而且我很难为fetchAssets方法获取localIdentifiers。 XCode gives me error "Cannot convert value of type '([PHCloudIdentifier]) -> [String]' to expected argument type '[String]'". XCode给我错误“无法将类型'([[PHCloudIdentifier])-> [String]'的值转换为预期的参数类型'[String]'”。 There are many code samples availbale for iOS but much lees for macOS. iOS上有许多代码示例可用,而macOS则有很多不足。 Is anyone willing to share some codes and/or provide any hints? 是否有人愿意分享一些代码和/或提供任何提示? Thanks. 谢谢。

Well if all you are trying to do is access the photos then to open the Photo app just add this code snippet to your controller 好吧,如果您要做的就是访问照片,然后打开“照片”应用程序,只需将此代码段添加到您的控制器即可

override func viewDidLoad() {
    super.viewDidLoad()

    NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Photos.app"))
}

Also, see this thread: How to get all PHAssets from MacOS's Photo Library 另外,请参见以下主题: 如何从MacOS的照片库中获取所有PHAsset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM