UICollectionView won't reloadData() after UIIm

2019-08-26 19:54发布

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.

2条回答
手持菜刀,她持情操
2楼-- · 2019-08-26 20:39

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

查看更多
虎瘦雄心在
3楼-- · 2019-08-26 20:47

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()
            }
        }
    }
}
查看更多
登录 后发表回答