[英]How to display images that in photo gallery that were just selected?
Goal: The user selects photos from her photo gallery > Tap "Done" > Photos show up. 目标:用户从她的相册中选择照片>点击“完成”>显示照片。
Problem: The photos don't show up immediately after "Done". 问题: “完成”后,照片不会立即显示。 They do show up if I open the picker again and tap "Cancel."
如果我再次打开选择器并点击“取消”,它们就会显示出来。
This worked fine (photo showed up instantly) when I was using UIImagePickerController, but then I switched to ELCImagePickerController for multiple selection and it no longer works. 当我使用UIImagePickerController时,这种方法效果很好(照片立即显示出来),但是后来我切换到ELCImagePickerController进行了多次选择,因此它不再起作用。
Hunches 预感
collectionView.reloadData()
after the images are selected (on the completionHandler). collectionView.reloadData()
后尝试调用collectionView.reloadData()
)。 This did not work. If helpful, this is my elcImagePickerController
function from my ViewController.swift
如果有帮助,这是我的
ViewController.swift
elcImagePickerController
函数
func elcImagePickerController(picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info: [AnyObject]!) {
println("inside elcImagePickerController")
self.dismissViewControllerAnimated(true, completion: nil)
if (info.count == 0) {
return
}
var pickedImages = NSMutableArray()
for any in info {
let dict = any as! NSMutableDictionary
let image = dict.objectForKey(UIImagePickerControllerOriginalImage) as! UIImage
pickedImages.addObject(image)
}
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), {
PHPhotoLibrary.sharedPhotoLibrary().performChanges(
{
for img in pickedImages{
// Request creating an asset from the image
// create multiple creationRequestForAssetFromImage
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(img as! UIImage) // add to main gallery
// / Request editing the album
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset)
// Get a placeholder for the new asset and add it to the album editing request
let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
albumChangeRequest.addAssets([assetPlaceholder])
}
}, completionHandler: {(success, error) in
NSLog("Adding Image to Library -> %@", (success ? "Success" : "Error"))
picker.dismissViewControllerAnimated(true, completion: nil)
}
)
})
It took me a while to duplicate your scenario and error, but I managed and I found a solution! 我花了一段时间来复制您的情况和错误,但是我设法解决了,找到了解决方案!
You were on the right path with calling collectionView.reloadData()
in the completionHandler
, but it needed a bit more work. 你是用呼唤正确的道路上
collectionView.reloadData()
在completionHandler
,但它需要更多的工作。
1) 1)
In my testing scenario, I've used a PHFetchResult
called photosAsset
as the main source for the UICollectionView
(I'm presuming you have a similar setup). 在我的测试场景中,我用了一个
PHFetchResult
称为photosAsset
作为主要来源UICollectionView
(我假设你有一个类似的设置)。
You have to update this PHFetchResult
to reflect the recent changes, in my case I used the following line: 您必须更新此
PHFetchResult
以反映最近的更改,在我的情况下,我使用了以下行:
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
2) 2)
It's correct that you have to reload the view with collectionView.reloadData()
, but the code inside of the completionHandler
is not run on the main thread/UI thread. 这是正确的,你必须重新加载与视图
collectionView.reloadData()
但里面的代码completionHandler
没有主线程/ UI线程上运行。 So you have to call it like the following: 因此,您必须按以下方式调用它:
dispatch_async(dispatch_get_main_queue(), {self.collectionView?.reloadData()})
Sum up: 总结:
In final, your completionHandler
should look something similar to this: 最后,您的
completionHandler
应该看起来类似于以下内容:
completionHandler: {(success, error) in
NSLog("Adding Image to Library -> %@", (success ? "Success" : "Error"))
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
dispatch_async(dispatch_get_main_queue(), {self.collectionView?.reloadData()})
picker.dismissViewControllerAnimated(true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.