1

我想以编程方式在 iOS 应用程序的照片库中的照片中添加/删除关键字。我已经编写并测试了下面的代码来切换给定照片上的收藏设置,但我无法在文档中找到有关关键字的任何内容。照片显然具有包含关键字的元数据。

这是在库中的最新照片上切换“收藏”的简单功能,此代码确实按预期完成。

class ViewController: UIViewController {
    @IBOutlet weak var myImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        photoAuthorization()
        toggleFavorite()
    }

    private func loadImage() -> UIImage? {

        let manager = PHImageManager.default()
        let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions())

        var image: UIImage? = nil
        manager.requestImage(for: fetchResult.object(at: 0), targetSize: CGSize(width: 800, height: 600), contentMode: .aspectFill, options: requestOptions() ) {img, err in
            guard let img = img else { return }
            image = img
        }
        return image

    }//load Image

    private func fetchOptions() -> PHFetchOptions {
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        return fetchOptions
    }//fetch options

    private func requestOptions() -> PHImageRequestOptions {
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true
        requestOptions.deliveryMode = .highQualityFormat
        return requestOptions
    }//request options

    private func photoAuthorization() {
        let status = PHPhotoLibrary.authorizationStatus()

        switch status {
        case .authorized:
            myImageView.image = loadImage()
        case .restricted, .denied:
            print("put up alert for auth restricted or denied")
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization { (status) in
                switch status {
                case .authorized:
                    DispatchQueue.main.async {
                        self.myImageView.image = self.loadImage()
                    }
                case .restricted, .denied:
                    print("in notDetermined put up alert for auth restricted or denied")
                case .notDetermined:
                    break
                @unknown default:
                    fatalError("fatal error inside request authorization (inner loop)")
                }
            }
        @unknown default:
            fatalError("fatal error authorized switch (outer loop)")
        }
    }//photo authorization

    private func toggleFavorite() {

        let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: fetchOptions())
        guard let firstAsset = fetchResult.firstObject else { return }

        PHPhotoLibrary.shared().performChanges({
            let changeRequest = PHAssetChangeRequest(for: firstAsset)
            changeRequest.isFavorite = !firstAsset.isFavorite

            //want to add keyword instead of toggling favorite
            //changeRequest.setValue("jumping", forKey: "keywords")
            //
        }) { (success, error) in
            print(success ? "Successfully updated favorite" : "\(error ?? "error did not work either" as! Error)")
        }
    }//toggle favorite

}//class

任何指导将不胜感激。Xcode 11.4.1

4

0 回答 0