Delete images in camera roll from code iOS

后端 未结 2 2093
深忆病人
深忆病人 2020-12-30 17:18

Is there a way to delete images (and videos) in camera roll in the photos app that my app didn\'t create. I know you can\'t delete things from Asset Library that your app di

2条回答
  •  感动是毒
    2020-12-30 17:57

    Here is a version for Swift that will delete all photos in the library.

    First, you will need to be sure that you have the key for permissions in your app's info.plist file:

    You will need to have been given permission by the user to access Photos (not including code here for that).

    Next, the import & code:

    import Photos
    
    func deleteAllPhotos() {
        let library = PHPhotoLibrary.shared()
        library.performChanges({
            let fetchOptions = PHFetchOptions()
            let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
            PHAssetChangeRequest.deleteAssets(allPhotos)
        }) { (success, error) in
            // Handle success & errors
        }
    }
    

    When this code is called, the user receives an OS prompt asking to confirm the deletion. Assuming they click yes, then everything is gone.

提交回复
热议问题