UIImagePickerController 关闭后 UICollectionView 不会重新加载数据() [英] UICollectionView won't reloadData() after UIImagePickerController dismisses
问题描述
我有一个小问题,从 UIImagePickerController 中选择图像后(并保存到 Parse,因为我的 UICollectionView 从我在 Parse 上的图像填充),我的 UICollectionView 不会加载新保存的图像,直到我离开该 viewController 并返回它.我已经尝试了几乎所有我能想到的工作.我应该在哪里放置我的代码来保存和关闭控制器?
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.
推荐答案
您的 lookUpPhotos
功能很好,但您可以在查询结束时更新 collectionView
.
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()
}
}
}
您可以在发送数据后再次调用 lookUpPhotos
函数.
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()
}
}
}
}
这篇关于UIImagePickerController 关闭后 UICollectionView 不会重新加载数据()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!