问题
此代码可将图像设置为数组,但会出现以下错误 - 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/