将照片添加到阵列并显示到UICollection查看错误

时间:2017-12-06 14:56:00

标签: swift uicollectionview uicollectionviewcell

问题

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

我需要知道这意味着什么,以及周围是否有可能修复?

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!

    }

}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我猜你没有要求照片/相机授权。 您必须将以下键添加到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 */ 
    } 
})