2015-07-12 73 views
1

我創建了一個PHAssetCollection來容納我的應用程序的照片,並且工作正常。但是,我試圖讓它可以讓用戶在按下按鈕時刪除PHAssetCollection。我如何去刪除我在下面的代碼中創建的整個AssetCollection(「App Folder」)?刪除PHAssetCollection(Swift)



代碼來創建PHAssetCollection:

let albumName = "App Folder" 

    //Check if the folder exists, if not, create it 
    let fetchOptions = PHFetchOptions() 
    fetchOptions.predicate = NSPredicate(format: "title = %@", albumName) 
    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions) 

    if let first_Obj:AnyObject = collection.firstObject{ 
     //found the album 
     self.albumFound = true 
     self.assetCollection = first_Obj as! PHAssetCollection 
    }else{ 
     //Album placeholder for the asset collection, used to reference collection in completion handler 
     var albumPlaceholder:PHObjectPlaceholder! 
     //create the folder 
     NSLog("\nFolder \"%@\" does not exist\nCreating now...", albumName) 
     PHPhotoLibrary.sharedPhotoLibrary().performChanges({ 
      let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName) 
      albumPlaceholder = request.placeholderForCreatedAssetCollection 
      }, 
      completionHandler: {(success:Bool, error:NSError!)in 
       if(success){ 
        println("Successfully created folder") 
        self.albumFound = true 
        if let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceholder.localIdentifier], options: nil){ 
         self.assetCollection = collection.firstObject as! PHAssetCollection 
        } 
       }else{ 
        println("Error creating folder") 
        self.albumFound = false 
       } 
     }) 

    } 

回答

1
PHPhotoLibrary.sharedPhotoLibrary().performChanges({() -> Void in 

    PHAssetCollectionChangeRequest.deleteAssetCollections([self.deleteTarget]) 

    }, completionHandler: nil) 
1

有一類方法PHAssetCollectionChangeRequest稱爲deleteAssetCollections:它做到了這一點:要求將特定資源集合中刪除。縱觀文件似乎可以只是PHAssetCollections數組像這樣調用這個:

PHAssetCollectionChangeRequest.deleteAssetCollections(self.assetCollection) 
+1

這沒有工作,但有人靠近,它需要一個完成處理程序...我發現在我的解決辦法更新的答案。 –