简体   繁体   English

如何显示照片库中刚选择的图像?

[英]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 that displays all images is not being reloaded? 显示所有图像的CollectionView是否未重新加载? But I tried calling collectionView.reloadData() after the images are selected (on the completionHandler). 但是我选择了图像(在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.

相关问题 如何在CollectionView中显示照片库或相机中的选定图像? 图库视图 - How can I display selected images from the Photo Library or Camera in a CollectionView? Gallery view 如何在uiwebview ios swift上显示照片库中的图像? - How to display images from Photo gallery on uiwebview ios swift? 显示从图库到表视图中选择的照片 - Display Photo Selected From Gallery into Table View 如何在收藏夹视图中显示从图库中选择的多个图像? - How to display multiple images selected from gallery in collection view? 如何将滤镜应用于照片库中的选定图像,而不仅仅是硬编码的图像 - How do I go about applying filters to selected images from the photo library not just hardcoded ones 如何为画廊中的多个图像传递图像数组并将其显示在Flutter中的另一个小部件上 - How to pass an array of images for selected multiple images from gallery and display them on another widget in Flutter 使用 BSImagePicker Swift4 在图库中显示选定的图像 - Show just selected images in gallery using BSImagePicker Swift4 如何在iOS 8 Swift中从照片库中提取多个图像 - How to pickup multiple images from photo gallery in iOS 8 Swift 如何制作图像拼贴并将图像保存到照片库 - How to make Collage of Images and save image to Photo Gallery MFMailComposeViewController:附加照片库中的图像 - MFMailComposeViewController: Attaching Images from Photo Gallery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM