How to delete the last photo in Swift iOS8?

你离开我真会死。 提交于 2019-12-02 07:15:21

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.

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