将图像从集合传递到ViewController Swift

时间:2019-02-02 06:41:48

标签: swift xcode firebase tableview

我遇到问题,无法使用Firebase数据库将ImageView从集合传递到ViewController

此我的代码

导入UIKit 导入Firebase 进口FirebaseDatabase 导入SDWebImage 进口FirebaseStorage 导入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“”“


导入基金会 进口FirebaseDatabase

结构ImagePost {     让关键:字符串!     让url:String!

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()


}

}


和感谢您的帮助家伙!

1 个答案:

答案 0 :(得分:0)

您可以通过使用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并将数据传递给它,如上所示。