【问题标题】:Adding Photos to Array and Displaying to UICollection View error将照片添加到数组并显示到集合视图错误
【发布时间】:2017-12-06 14:56:00
【问题描述】:

问题

此代码用于将图像设置为数组,但出现以下错误 - 2017-12-06 12:31:21.264812+0000 SmartReceipts[880:172369] [discovery] 错误发现扩展时遇到:Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

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

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!

    }

}

【问题讨论】:

  • 错误发生在哪一行?你能设置一个断点并检查一下吗。

标签: swift uicollectionview uicollectionviewcell


【解决方案1】:

错误可能来自未请求授权从相机或照片库加载照片。所以你需要在打开相机之前请求授权,比如:

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

【讨论】:

    【解决方案2】:

    我猜你没有请求照片/相机授权。 您必须将以下键添加到 Info.plist。

    相机权限:

    <key>NSCameraUsageDescription</key>
    <string> ${PRODUCT_NAME} Camera Usage< </string>
    

    对于照片库,您将希望这个允许应用用户浏览照片库。

    <key>NSPhotoLibraryUsageDescription</key>
    <string>${PRODUCT_NAME} Photo Usage</string>
    

    在您的源代码中,要请求照片/相机的许可,您需要添加以下代码(Swift 3):

    PHPhotoLibrary.requestAuthorization({ 
           (newStatus) in 
             if newStatus ==  PHAuthorizationStatus.authorized { 
              /* do stuff here */ 
        } 
    })
    

    【讨论】:

    • 已完成但仍然出现错误,它要求许可,当单击图像时仍然出现该错误??
    • 你需要在func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])末尾关闭照片选择器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2013-08-02
    • 1970-01-01
    • 2017-08-25
    相关资源
    最近更新 更多