问题描述
在Mac OS X中加载mediaSources
属性时出现错误.
我正在尝试使用MLMediaLibrary
类获取Apple Photos源.
我的应用已沙箱化,对图片文件夹具有只读权限.
我遇到了错误:
I'm getting an error while loading the mediaSources
property in Mac OS X.
I'm trying to get the Apple Photos source using the MLMediaLibrary
class.
My app is sandboxed, and has the read only permission for the pictures folder.
I'm getting the error:
根据我的收集,错误4097被中断了.
我对Swift不太熟悉,但是我确实使用目标C进行了完全相同的测试,并获得了相同的结果.
我的猜测是我缺少某种权利.
这是我的代码(非常简化):
From what I gather, error 4097 is connection interrupted.
I'm not very familiar with Swift, but I did run the exact same test using objective C and got the same result.
My guess is that I'm missing an entitlement of some sort.
Here's my (very simplified) code:
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
Turns out there were two things amiss:
1. The MLMediaLoadIncludeSourcesKey
should point to an array of strings, not a single string
2. The observation key path was wrong, should be "mediaSources"
, was missing an s
这篇关于在MLMediaLibrary中加载媒体源时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!