swift - 将照片添加到阵列并显示到 Collection View 错误

标签 swift uicollectionview uicollectionviewcell

问题

此代码可将图像设置为数组,但会出现以下错误 - 2017-12-06 12:31:21.264812+0000 SmartReceipts[880:172369] [discovery] 错误发现扩展时遇到:Error Domain=PlugInKit Code=13“查询已取消”UserInfo={NSLocalizedDescription=查询已取消}

我需要知道这意味着什么以及是否有可能解决它?

import UIKit

class GalleryController: UICollectionViewController,
                         UIImagePickerControllerDelegate,
                         UINavigationControllerDelegate {

    var Receipts = [UIImage?]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView?.reloadData()





        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        //self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        self.dismiss(animated: true, completion: nil)
        print(info);
        let newReceipts = info[UIImagePickerControllerEditedImage] as? UIImage
        self.Receipts.append(newReceipts)

        print("Array Contains \(self.Receipts.count) Receipts")
        print(self.Receipts)
        self.collectionView?.reloadData()

        print("completedIfStatement")
        }







    @IBAction func getReceipts(_ sender: Any) {
        print("PlusButtonPressed")

        let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .actionSheet)

        // 2
        let albumAction = UIAlertAction(title: "Album", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            print("PhotosOption")
            self.getFromReceipts()
        })
        let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            print("CameraOption")
            self.getFromCamera()
        })

        //
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
            (alert: UIAlertAction!) -> Void in
            print("Cancel")
        })


        // 4
        optionMenu.addAction(albumAction)
        optionMenu.addAction(cameraAction)
        optionMenu.addAction(cancelAction)

        // 5
        self.present(optionMenu, animated: true, completion: nil)

    }


    func getFromReceipts() {

        print("GetFromReceipts")
        let cameraPicker = UIImagePickerController()
        cameraPicker.delegate = self
        cameraPicker.sourceType = .photoLibrary

        self.present(cameraPicker, animated: true, completion: nil )


    }

    func getFromCamera() {
        print("GetFromCamera")
        let cameraPicker = UIImagePickerController()
        cameraPicker.delegate = self
        cameraPicker.sourceType = .camera

        self.present(cameraPicker, animated: true, completion: nil )

    }



    //Number of Views
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.Receipts.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtindexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Receipts, for: indexPath as IndexPath) as? PhotoCell


        cell?.imageView.image = self.Receipts[indexPath.row]
        return cell!

    }

}

最佳答案

该错误可能是由于未请求从相机或照片库加载照片的授权。因此,您需要在打开相机之前请求授权,例如:

https://developer.apple.com/documentation/photos/phphotolibrary/1620736-requestauthorization

关于swift - 将照片添加到阵列并显示到 Collection View 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47677232/

相关文章:

ios - 如何从 Nuke 中删除图像以用于 Swift 缓存

json - 无法将字符串类型的值转换为预期的参数类型 Int

swift - 将 UI BarButtonSystemItem 图标添加到 Nav-Toolbar (SWIFT)

ios - UICollectionView UIRefreshControl 在访问不同的 View 后不会动画

ios - 考虑到性能,如何在 UICollectionViewCell 中旋转 UILabel?

ios - CollectionView 标题在自动旋转时不会调整大小

ios - 在自定义子类 UICollectionViewCell 类中添加 subview

具有动态高度的 iOS 单元格宽度不佳

ios - 如何将计数按钮打印到标签上并永久保存? swift 3

ios - TableViewCell 的几个问题