微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 在MLMediaLibrary中加载媒体源时出错

我在Mac OS X中加载mediaSources属性时出错.
我正在尝试使用MLMediaLibrary类获取Apple Photos源代码.
我的应用程序是沙盒,并具有图片文件夹的只读权限.
我收到错误

MLMediaLibrary error obtaining remote object proxy: Error Domain=NSCocoaErrorDomain Code=4097 “connection to service named com.apple.MediaLibraryService” UserInfo={NSDebugDescription=connection to service named com.apple.MediaLibraryService}

根据我的收集,错误4097是连接中断.
我对Swift不是很熟悉,但我确实使用目标C进行了完全相同的测试并获得了相同的结果.
我的猜测是我错过了某种权利.

这是我的(非常简化的)代码

import Foundation
import MediaLibrary

public class MediaLibrary : NSObject{
    var library : MLMediaLibrary!

    private func loadSources(){
        if let mediaSources = library.mediaSources {

            for (ident,source) in mediaSources{
                print("Identifier: \(ident)");
            }
        }

    }

    public override func observeValueForKeyPath(keyPath: String?,ofObject object: AnyObject?,change: [String : AnyObject]?,context: UnsafeMutablePointer<Void>) {
        loadSources()
    }

    public override init(){
        super.init()
        let options : [String : AnyObject] = [MLMediaLoadSourceTypesKey : MLMediaSourceType.Image.rawValue,MLMediaLoadIncludeSourcesKey : MLMediaSourcePhotosIdentifier]
        library = MLMediaLibrary(options: options)
        library.addobserver(self,forKeyPath: "mediaSource",options: NSkeyvalueObservingOptions.New,context: nil)
        library.mediaSources; // trigger load,status will be reported back in observeValueForKeyPath
    }
}

解决方法

原来有两件事不对劲: 1. MLMediaLoadIncludeSourcesKey应指向一个字符串数组,而不是单个字符串 2.观察的关键路径是错误的,应该是“mediaSources”,缺少一个s

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐