问题
I try to get the last photo from camera roll and delete it.Now I get the last photo but have problems in deleting the last photo. I tried this way but I delete all photos,so I plan to build a new PHFetchResult which only include the last photo but I don't know how to do that.
PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
PHAssetChangeRequest.deleteAssets(fetchResult)},
completionHandler: {
success, error in
NSLog("Finished deleting asset. %@", (success ? "Success" : error))
})
Thank all of you to answer my question!
回答1:
I guess the problem lies in your fetchResult
.
You will have to pass an array that contains only latest image from photo library.
Try making that array as follows-
var fetchOptions: PHFetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
var fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
if (fetchResult.lastObject != nil) {
var lastAsset: PHAsset = fetchResult.lastObject as PHAsset
let arrayToDelete = NSArray(object: lastAsset)
PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
PHAssetChangeRequest.deleteAssets(arrayToDelete)},
completionHandler: {
success, error in
NSLog("Finished deleting asset. %@", (success ? "Success" : error))
})
}
See this link.
来源:https://stackoverflow.com/questions/29466866/how-to-delete-the-last-photo-in-swift-ios8