UICollectionView won't reloadData() after UIImagePickerController dismisses

纵然是瞬间 提交于 2019-12-13 06:46:28

问题


I have a small issue in that after selecting an image from UIImagePickerController (and saving to Parse because my UICollectionView populates from my images on Parse), my UICollectionView will not load the newly saved image until I leave that viewController and go back to it. I've tried just about every work around that I can think of. Where should I be putting my code to save and dismiss controller?

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    let imagePicture = UIImageJPEGRepresentation(image, 0.1)
    let imageFile = PFFile(name: "imageFile.jpg", data: image)

    let saveImage = PFObject(className: "Gallery")
    saveImage["imageFile"] = imageFile
    saveImage["dogName"] = self.dogSelectedName
    saveImage["userID"] = PFUser.currentUser()?.objectId
    saveImage["dogID"] = self.dogObjectID
    saveImage.saveInBackgroundWithBlock { (Bool, error) -> Void in
        if error != nil {
            print("There was an error")
        } else {
            print("New Gallery object saved: \(saveImage)")
        }
    }

    self.collectionView.reloadItemsAtIndexPaths(self.collectionView.indexPathsForVisibleItems())
    self.collectionView.reloadData()

    self.dismissViewControllerAnimated(true) { () -> Void in}
}

After debugging, I notice a lot of the time it saves my object after the view appears (or depending where I write the code, before the view appears) and still will not reload the collection view until I leave and come back. I've put:

dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.collectionView.reloadData()
    }

In multiple places in my code but still same result.. How else can/should I be setting this up? Any help is lots of help!! Thanks.


回答1:


Your lookUpPhotos function is fine, but you can update the collectionView at the end of your query.

func lookUpPhotos() {

    let findPhotos = PFQuery(className: "Gallery")
    findPhotos.whereKey("dogID", equalTo: self.dogObjectID)
    findPhotos.orderByDescending("createdAt")
    findPhotos.findObjectsInBackgroundWithBlock { 
        (objects, error) -> Void in

        if let users = objects {
            for object in users { 
                self.photoData.addObject(object)
                print("Fetched \(self.dogSelectedName)'s \(self.photoData.count) Images") 
            } 
        }

        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.collectionView.reloadData()
        }
    }
}

You can then call the lookUpPhotos function again once you send the data.

saveImage.saveInBackgroundWithBlock { (Bool, error) -> Void in
    if error != nil {
        print("There was an error")
    } else {
        print("New Gallery object saved: \(saveImage)")
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.dismissViewControllerAnimated(true) { () -> Void in
                self.lookUpPhotos()
            }
        }
    }
}



回答2:


You saved it in Parse, but it doesn't seem like you're adding it to your dataSource.

So if your UICollectionView's data source of Dogs or DogImages is an array called

var dogArray = []

You need to add your dog to that array in didFinishPickingImage

dogArray.append(imagePicture)

Hope it helps



来源:https://stackoverflow.com/questions/34366830/uicollectionview-wont-reloaddata-after-uiimagepickercontroller-dismisses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!