在MLMediaLibrary中加载媒体源时出错 [英] Error loading media sources in MLMediaLibrary

查看:212
本文介绍了在MLMediaLibrary中加载媒体源时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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:

获取远程对象代理的MLMediaLibrary错误:错误Domain = NSCocoaErrorDomain代码= 4097连接到名为com.apple.MediaLibraryService的服务" UserInfo = {NSDebugDescription =连接到名为com.apple.MediaLibraryService的服务}

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进行了完全相同的测试,并获得了相同的结果.
我的猜测是我缺少某种权利.

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

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中加载媒体源时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆