0

目标:用户从她的照片库中选择照片 > 点击“完成”> 照片显示。

问题: “完成”后照片没有立即显示。如果我再次打开选择器并点击“取消”,它们就会出现。

当我使用 UIImagePickerController 时,这很好用(照片立即显示),但后来我切换到 ELCImagePickerController 进行多项选择,它不再起作用。

预感

  • 显示所有图像的 CollectionView 没有被重新加载?但是我尝试collectionView.reloadData()在选择图像后调用(在completionHandler上)。这没有用。

如果有帮助,这是我的elcImagePickerController功能ViewController.swift

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

1 回答 1

1

我花了一段时间来复制您的场景和错误,但我设法并找到了解决方案!

您在调用 时走在正确的道路上collectionView.reloadData()completionHandler但需要做更多的工作。


1)

在我的测试场景中,我使用了一个PHFetchResult被调用photosAsset的作为主要来源UICollectionView(我假设你有一个类似的设置)。

您必须更新它PHFetchResult以反映最近的更改,在我的情况下,我使用了以下行:

self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)

2)

您必须使用 重新加载视图是正确的collectionView.reloadData(),但其中的代码completionHandler不在主线程/UI 线程上运行。所以你必须这样称呼它:

dispatch_async(dispatch_get_main_queue(), {self.collectionView?.reloadData()})

总结:

最后,您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)
}
于 2015-06-06T21:12:19.863 回答