How to delete the last photo in Swift iOS8?

末鹿安然 提交于 2019-12-04 05:22:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!