如何以编程方式从Mac Photo的应用程序中删除照片,并将更改也适用于iCloud?我正在开发一个应用程序,它应该根据许多标准将照片移动到垃圾箱。
我首先使用MLMediaLibrary API查找图像,这些图像有效:
class PhotoFinder : NSObject {
var _onLoaded: ([PhotoReference]) -> Void
var _mediaLibrary : MLMediaLibrary
var _observerAdded : Bool
var _loading : Bool
var _mediaSource: MLMediaSource!
var _album: MLMediaGroup!
init(onLoaded: ([PhotoReference]) -> Void) {
let options: [String : AnyObject] = [
MLMediaLoadSourceTypesKey: MLMediaSourceType.Image.rawValue,
MLMediaLoadIncludeSourcesKey: [MLMediaSourcePhotosIdentifier]
];
_onLoaded = onLoaded;
_mediaLibrary = MLMediaLibrary(options: options);
_observerAdded = false
_loading = false
super.init();
}
func load() {
if (_loading) {
return
}
if (!_observerAdded) {
_mediaLibrary.addObserver(self, forKeyPath: "mediaSources", options: NSKeyValueObservingOptions.New, context: nil);
_observerAdded = true;
}
_loading = true
_mediaLibrary.mediaSources
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard let path = keyPath else { return }
switch path {
case "mediaSources":
loadSources()
case "rootMediaGroup":
loadAlbums()
case "mediaObjects":
loadMediaObjects()
default:
print("Nothing to do for \(path)")
}
}
func loadSources() {
if let mediaSources = _mediaLibrary.mediaSources {
_mediaSource = mediaSources["com.apple.Photos"]
_mediaSource.addObserver(self, forKeyPath: "rootMediaGroup", options: NSKeyValueObservingOptions.New, context: nil)
_mediaSource?.rootMediaGroup
}
}
func loadAlbums() {
let albums: MLMediaGroup = _mediaSource.mediaGroupForIdentifier("TopLevelAlbums")!
for album in albums.childGroups! {
let identifier = album.attributes["identifier"]
if (identifier?.isEqualToString("allPhotosAlbum") == true) {
_album = album
_album.addObserver(self, forKeyPath: "mediaObjects", options: NSKeyValueObservingOptions.New, context: nil)
_album.mediaObjects
break
}
}
}
func loadMediaObjects() {
var photos = [PhotoReference]()
for photo in _album.mediaObjects! {
photos.append(PhotoReference(identifier: photo.identifier, originalPath: photo.originalURL!.path!))
}
_onLoaded(photos)
_loading = false
}
}
我在官方文档中找不到相关信息。