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