簡體   English   中英

從從Firebase數據庫加載的UICollectionView中保存照片

[英]Saving Photos from UICollectionView loaded from Firebase Database

我的galleryCollectionView當前正在使用Firebase數據庫中的圖像填充。

我在弄清楚如何允許用戶長按圖像並將所選照片保存到照片庫時遇到麻煩。 目前,我目前仍在努力爭取長按。

這是我的ViewController (我正在使用Swift 4):

class PhotoGalleryViewController: UIViewController, UICollectionViewDataSource, UIGestureRecognizerDelegate {

    @IBOutlet weak var galleryCollectionView: UICollectionView!

    var images = [GalleryImages]()
    var customImageFlowLayout: CustomGalleryCollectionFlowLayout!
    var dbRef: DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        let photoLngPrs = UILongPressGestureRecognizer(target: self, action: "handleLongPress")
        photoLngPrs.minimumPressDuration = 0.5
        photoLngPrs.delegate = self
        photoLngPrs.delaysTouchesBegan = true
        self.galleryCollectionView.addGestureRecognizer(photoLngPrs)
        dbRef = Database.database().reference().child("photoGallery")
        customImageFlowLayout = CustomGalleryCollectionFlowLayout()
        galleryCollectionView.collectionViewLayout = customImageFlowLayout
        galleryCollectionView.backgroundColor = UIColor.black
        loadGalleryDB()
        // Do any additional setup after loading the view.
    }

    func loadGalleryDB() {
        dbRef.observe(DataEventType.value, with: {(snapshot) in
            var newGalleryImages = [GalleryImages]()
            for galleryImagesSnapshot in snapshot.children {
                let galleryImagesObject = GalleryImages(snapshot: galleryImagesSnapshot as! DataSnapshot )
                newGalleryImages.append(galleryImagesObject)
            }



            self.images = newGalleryImages
            self.galleryCollectionView.reloadData()
        })
    }

    func handleLongPress(gestureRecognizer : UILongPressGestureRecognizer) {
        if gestureRecognizer.state == UIGestureRecognizerState.began {
            return
        }
        let point = gestureRecognizer.location(in: self.galleryCollectionView)
        let indexPath = galleryCollectionView.indexPathForItem(at: point)
        if let index = indexPath {
            var collectionCell = galleryCollectionView.cellForItem(at: index)
            print(index.row)
        } else {
            print("Could not find Index Path")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "galleryCell", for: indexPath) as! GalleryCollectionViewCell
        let image = images[indexPath.row]
        cell.galleryView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "NightCLubPhoto"))

        cell.galleryView.isUserInteractionEnabled = true

        return cell
}

我收到此錯誤。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Hookaholics.PhotoGalleryViewController handleLongPress]: unrecognized selector sent to instance 0x7fa61bc0d7a0'

這是我針對與您類似情況的解決方案。

您創建一個UILongPressGestureRecognizer並將其添加到CollectionView

func createLongPressGesture(){

    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressOccured(sender:)))
    longPress.minimumPressDuration = 0.7 // Default `minimumPressDuration` is 0.5. It sets the time after which UILongPressGestureRecognizer enters the 'began' state.
    longPress.isEnabled = true
    self.galleryCollectionView.addGestureRecognizer(longPress)

}

您可以在viewDidLoad校准此函數。

下一步是實現選擇器:

@objc func longPressOccured(sender:UILongPressGestureRecognizer){

    guard sender.state == .began else{ return }
    guard let pressedIndexPath = self.galleryCollectionView.indexPathForItem(at: sender.location(in: self.galleryCollectionView)) else { return }
    guard let pressedCell = self.galleryCollectionView.cellForItem(at: pressedIndexPath) else { return }
    let cell = pressedCell as! GalleryCollectionViewCell

    // HERE comes your code for saving the selected image

}

希望我能解決您的問題,我的解決方案會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM