简体   繁体   English

UIImagePickerController关闭后,UICollectionView将不会reloadData()

[英]UICollectionView won't reloadData() after UIImagePickerController dismisses

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. 我有一个小问题,那就是从UIImagePickerController中选择一个图像(并保存到Parse,因为我的UICollectionView是从Parse上的图像中填充)之后,UICollectionView才会加载新保存的图像,直到我离开该viewController并返回它为止。 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. 谢谢。

Your lookUpPhotos function is fine, but you can update the collectionView at the end of your query. 您的lookUpPhotos函数很好,但是您可以在查询结束时更新collectionView

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. 发送数据后,您可以再次调用lookUpPhotos函数。

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

You saved it in Parse, but it doesn't seem like you're adding it to your dataSource. 您已将其保存在Parse中,但似乎没有将其添加到dataSource中。

So if your UICollectionView 's data source of Dog s or DogImage s is an array called 因此,如果您的UICollectionViewDogDogImage的数据源是一个名为

var dogArray = []

You need to add your dog to that array in didFinishPickingImage 您需要在didFinishPickingImage中将狗添加到该数组中

dogArray.append(imagePicture)

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM