iOS Swift 3 - 如何删除照片中最近删除的图像

标签 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
    }
}

最佳答案

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

关于iOS Swift 3 - 如何删除照片中最近删除的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43099176/

相关文章:

ios - 使用 inputAccessoryView 在 UICollectionViewController 上的第 0 个索引中插入项目时出现问题

ios - swift 4 : cellForRowAt skipping last row

ios - 当应用程序准备好显示警报时,从 AppDelegate 显示警报

CSS:如何在背景图像上创建线条或点?

html - 如何在 CSS 中使图像覆盖?

ios - StreamDelegate 在快速读取大量读数后停止接收事件

ios - 仅在单击 'Save' 按钮时保存录制的视频

ios - 第二个 UIWindow 中的方向更改

ios - Sqlite 遍历所有表

html - HTML 是否允许按流对图像进行嵌入式编码?