【问题标题】:Pass image from collection to viewcontroller swift将图像从集合快速传递到视图控制器
【发布时间】:2019-02-02 06:41:47
【问题描述】:

我有一个问题,我无法使用 Firebase 数据库将 ImageView 从集合传递到视图控制器

这是我的代码

导入 UIKit 导入 Firebase 导入 Firebase 数据库 导入 SDWebImage 导入 Firebase 存储 导入 KRProgressHUD

类 ViewControllerCollocation: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var MyColloctaion: UICollectionView!


var imagePicker = UIImagePickerController()

var images = [ImagePost]()


var dbRef:DatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()

    setutxent()

    designCollectionView()

    imagePicker.delegate = self

    dbRef = Database.database().reference().child("Images")



    /////////


    loadDB()

    KRProgressHUD.set(style: .custom(background: .black, text: .white, icon: nil))
    KRProgressHUD.show(withMessage: "Loading...")

    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {

        KRProgressHUD.dismiss()

    }


}

func loadDB(){

    dbRef.observe(DataEventType.value, with: { (snapshot) in

        var newImages = [ImagePost]()

        for imageFireSnapshot in snapshot.children {

            let ImagesFireObject = ImagePost(snapshot: imageFireSnapshot as! DataSnapshot)
            newImages.append(ImagesFireObject)
        }
        self.images = newImages
        self.MyColloctaion.reloadData()
    }
    )}


func collectionView(_ colloction: UICollectionView, numberOfItemsInSection section: Int) -> Int {


    return images.count
}


internal func collectionView(_ colloction: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = colloction.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! UploadOneCollectionViewCell


    let image = images[indexPath.row]

    cell.ImageView.layer.cornerRadius = 13
    cell.ImageView.clipsToBounds = true

    cell.ImageView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "Image1"))

    return cell


}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

func designCollectionView() {

    let itemSize = UIScreen.main.bounds.width/3 - 3

    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
    layout.itemSize = CGSize(width: itemSize, height: 230)

    layout.minimumInteritemSpacing = 1
    layout.minimumLineSpacing = 1
    //layout.scrollDirection = .horizontal

    self.MyColloctaion.isPagingEnabled = true


    MyColloctaion.collectionViewLayout = layout
}

func steupbar(){

    navigationController?.navigationBar.barTintColor = UIColor.black
    tabBarController?.tabBar.barTintColor = UIColor.black
    self.navigationController?.navigationBar.titleTextAttributes =
        [NSAttributedString.Key.foregroundColor: UIColor.white,
         NSAttributedString.Key.font: UIFont(name: "Baskerville-BoldItalic", size: 21)!]

}


@IBAction func Upload(_ sender: Any) {



    imagePicker =  UIImagePickerController()
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .photoLibrary
    imagePicker.delegate = self

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

func generateRandom(size: UInt) -> String {
    let prefixSize = Int(min(size, 43))
    let uuidString = UUID().uuidString.replacingOccurrences(of: "-", with: "")
    return String(Data(uuidString.utf8)
        .base64EncodedString()
        .replacingOccurrences(of: "=", with: "")
        .prefix(prefixSize))
}

}


这是我的""ImagePost.swift"""


导入基础 导入 Firebase 数据库

结构 ImagePost { 让键:字符串! 让网址:字符串!

let itemRef:DatabaseReference?


init(url:String, key:String) {
    self.key = key
    self.url = url
    self.itemRef = nil
}

init(snapshot:DataSnapshot) {
    key = snapshot.key
    itemRef = snapshot.ref


    let snapshotValue = snapshot.value as? NSDictionary

    if let imageUrl = snapshotValue?["url"] as? String {
        url = imageUrl
    }else{
        url = ""
    }

}

}


这是我的详细视图

导入 UIKit

类 VC1:UIViewController {

 var imageURL : String?

@IBOutlet weak var ImageView: UIImageView!



override func viewDidLoad() {
    super.viewDidLoad()


}

}


感谢你们的帮助!

【问题讨论】:

    标签: swift xcode firebase tableview


    【解决方案1】:

    您可以通过在 didselect 方法中获取图像将图像传递到详细视图:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
            let image = images[indexPath.row]
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "VC1") as! VC1
            vc.imageObject = images[indexPath.row]
            self.navigationController?.pushViewController(vc, animated: false)
    }
    

    只需在 VC1 中创建一个变量作为 imageObject 并将数据传递给它,如上所示。

    【讨论】:

    • 非常感谢您的帮助,但有一个错误我不知道如何解决。 \\\ 。无法将类型“ImagePost”的值分配给类型“UIImage”
    • 您需要像在这一行中那样提取图像 url:cell.ImageView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "Image1"))在 VC1 中也可以这样做
    • 如果您仍然遇到任何问题,请告诉我
    • 当我选择项目时,我在这里遇到崩溃和红色 \ vc.ImageView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "Image1")) // / 。这是我的 VC1 // var objPosts : ImagePost! + in viewDidAppear = ImageView.sd_setImage(with: URL(string: objPosts!.url))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    相关资源
    最近更新 更多