为什么我的图片没有显示在UIImageView中?

时间:2019-06-05 00:11:42

标签: ios swift xcode firebase-realtime-database

我想获得一个if语句,如果所选按钮对应于第一个图像视图,则将其设置为第一个图像,否则将其设置为第二个图像视图...但是,一旦我从image picker,它只是忽略它而继续前进,就像什么也没发生一样。 这是我的代码: (它在图像选择器控制器功能下...)

class UploadSubPostCell: UICollectionViewCell {
    @IBOutlet weak var previewStep: UIImageView!
}

class UploadViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var previewImage: UIImageView!
    @IBOutlet weak var postBtn: UIButton!
    @IBOutlet weak var selectBtn: UIButton!

    @IBOutlet weak var postscollectionview: UICollectionView!
    @IBOutlet weak var selectStepsBtn: UIButton!

    var picker = UIImagePickerController()
    var isThumbnailImage = true
    var subpostsArray = [UIImage]()
    var subposts = [SubPost]()
    var posts = [Post]()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavigationBarItems()
        picker.delegate = self
    }

    func setupNavigationBarItems() {
        navigationItem.title = "Upload"
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerEditedImage] as? UIImage {

            let path = IndexPath(item: 0, section: 0)
            let cell = self.postscollectionview.cellForItem(at: path) as? UploadSubPostCell

            if isThumbnailImage{
                previewImage.image = image
            } else {
                cell?.previewStep.image = image
            }
            selectBtn.isHidden = false
            selectStepsBtn.isHidden = false
            postBtn.isHidden = false
            if isThumbnailImage == false {
                subpostsArray.append(image)
                subposts.count + 1
                print("Appended image to array:", subpostsArray)
            }
        }
        picker.dismiss(animated: true, completion: nil)
    }

    @IBAction func selectStepPressed(_ sender: Any) {

        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        isThumbnailImage = false

        self.present(picker, animated: true, completion: nil)
    }


    @IBAction func selectPressed(_ sender: Any) {

        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        isThumbnailImage = true

        self.present(picker, animated: true, completion: nil)
    }

    @IBAction func addNewPressed(_ sender: Any) {
    }


    @IBAction func postPressed(_ sender: Any) {
        AppDelegate.instance().showActivityIndicator()

        let uid = Auth.auth().currentUser!.uid
        let ref = Database.database().reference()
        let storage = Storage.storage().reference(forURL: "gs://mobile-d9fcd.appspot.com")

        let key = ref.child("posts").childByAutoId().key
        let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
        let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)

        let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
            if error != nil {
                print(error!.localizedDescription)
                AppDelegate.instance().dismissActivityIndicator()
                return
            }
                imageRef.downloadURL(completion: { (url, error) in
                    if let url = url {
                        let feed = ["userID" : uid,
                                    "pathToImage" : url.absoluteString,
                                    "likes" : 0,
                                    "author" : Auth.auth().currentUser!.displayName!,
                                    "postID" : key] as [String : Any]

                        let postFeed = ["\(key)" : feed]

                        ref.child("posts").updateChildValues(postFeed)
                        AppDelegate.instance().dismissActivityIndicator()
                        self.picker.dismiss(animated: true, completion: nil)
                        }
                    })
                }
            uploadTask.resume()
        }
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1

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

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell

            cell.postImage.downloadImage(from: self.posts[indexPath.row].pathToImage)
            cell.authorLabel.text = self.posts[indexPath.row].author
            cell.likeLabel.text = "\(self.posts[indexPath.row].likes!) Likes"
            cell.postID = self.posts[indexPath.row].postID
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果您使用的是Swift 4.2及更高版本,则应替换:

if let image = info[UIImagePickerControllerEditedImage] as? UIImage {

作者:

if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {

答案 1 :(得分:0)

didFinishPickingMediaWithInfo中,如果您知道图像放入了什么dataSource,则应将{strong>图像与cell一起保存。 ,请使用tableView.reloadRows(at indexPaths:)重新加载单元格。将图像添加到cell.previewStep.image

中的cellForRowAt()