iOS Swift 3 - 如何删除照片

时间:2017-03-29 16:29:43

标签: ios swift image swift3

我正在Swift 3中构建一个应用程序,它从现有图像导入图像,然后删除原始图像。 现在的问题是删除的图像移动到最近删除的文件夹中。那么,如何从最近删除的文件夹中删除已挑选的图像?

以下是我用来挑选和删除图片的代码。

import UIKit
import Photos

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate,
    UICollectionViewDelegate,
UICollectionViewDataSource{

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var collectionView: UICollectionView!
    var images: [UIImage] = [UIImage]()
    //var thumbnails:


    override func viewDidLoad() {
        super.viewDidLoad()


        self.collectionView.delegate = self
        self.collectionView.dataSource = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func addClick(_ sender: UIBarButtonItem) {
        self.click(sender)
    }
    @IBAction func click(_ sender: UIBarButtonItem) {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self

        let addActionSheet = UIAlertController.init(
            title: "Photo Source",
            message: "Choose a source",
            preferredStyle: UIAlertControllerStyle.init(rawValue: 1)!)

        if UIImagePickerController.isSourceTypeAvailable(.camera){
            addActionSheet.addAction(
                UIAlertAction.init(
                    title: "Camera",
                    style: .default,
                    handler: {
                        (action: UIAlertAction)in
                        imagePickerController.sourceType = .camera
                        imagePickerController.cameraCaptureMode = .photo
                        imagePickerController.modalPresentationStyle = .fullScreen
                        self.present(imagePickerController, animated: true, completion: nil)
                }
                )
            )
        }

        if(UIImagePickerController.isSourceTypeAvailable(.photoLibrary)){
            addActionSheet.addAction(
                UIAlertAction.init(
                    title: "Photo Library ",
                    style: .default,
                    handler: {
                        (action: UIAlertAction)in
                        imagePickerController.sourceType = .photoLibrary
                        self.present(imagePickerController, animated: true, completion: nil)
                }
                )
            )
        }

        addActionSheet.addAction(UIAlertAction.init(title: "Cancel ", style: .cancel, handler: nil))

        self.present(addActionSheet, animated: true, completion: nil)
    }

    /**
     *User picked a photo
     */
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        self.imageView.image = image
        self.images.append(image)

        self.collectionView.reloadData()
        picker.dismiss(animated: true, completion: nil)

        let imageURL = info[UIImagePickerControllerReferenceURL] as! URL
        let imageURLs = [imageURL]

        //Let's delete it now
        PHPhotoLibrary.shared().performChanges(
            //CHANGE-BLOCk
            {
                let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageURLs, options: nil)

                PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
        },
            completionHandler: {
                (success, error)in
                NSLog("Finished deleting asset. @", success ? "Success" : "Error")
        })
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

    /**
     *Returns the number of cells to create.
     */
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return self.images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        cell.layer.cornerRadius = 50
        cell.layer.borderColor = UIColor.blue.cgColor
        cell.layer.borderWidth = 3

        cell.image.image = self.images[indexPath.row]

        return cell
    }
}

1 个答案:

答案 0 :(得分:0)

您无法从“最近删除的”相册中删除照片,也许您应该向用户显示说明,以便他们可以转到“最近删除的”相册,选择其中的所有照片并将其删除。