使用BSImagePicker Swift4在图库中仅显示选定的图像

时间:2018-07-12 12:32:03

标签: ios swift swift4

我正在使用“ BSImagePicker”。但是我遇到了问题 第一次我要在图库中选择2张图像,再选择2张图像返回到viewController后,我又要在图库中选择图像,但看不到所选图像

我只想看相册中的选定图片。

这是我的BSImagePicker方法

let vc = BSImagePickerViewController()
    vc.maxNumberOfSelections = 2
    vc.takePhotos = true
    bs_presentImagePickerController(vc, animated: true,
                                    select: { (asset: PHAsset) -> Void in
                                        print("Selected: \(asset)")
    }, deselect: { (asset: PHAsset) -> Void in
        print("Deselected: \(asset)")
    }, cancel: { (assets: [PHAsset]) -> Void in
        print("Cancel: \(assets)")
    }, finish: { (assets: [PHAsset]) -> Void in
        print("Finish: \(assets)")
        print(assets.count)
        for i in 0..<assets.count {
            self.SelectedAssets.append(assets[i])
            print(self.SelectedAssets)
        }
    }, completion: nil)

任何帮助

2 个答案:

答案 0 :(得分:0)

为您的资产提供此行代码,它将为您返回图像。

您正在分派此方法。

print("Finish: \(assets)")
if let imageView = vc.imageView {
    PHCachingImageManager.default().requestImage(for: asset, targetSize:imageView.frame.size, contentMode: .aspectFit, options: options) { (result, _) in
                            imageView.image = result
                       }
 }

答案 1 :(得分:0)

我是这样完成的,可能是有人需要的话,所以我在这里发布答案

var SelectedAssets = [PHAsset]()

let assets = self.SelectedAssets
    let collection = PHAssetCollection.transientAssetCollection(with: assets, title: nil)
    let fetchedAssets = PHAsset.fetchAssets(in: collection, options: nil)

    let vc = BSImagePickerViewController()
    vc.maxNumberOfSelections = 2
    self.SelectedAssets.removeAll()
    vc.takePhotos = true
    vc.defaultSelections = fetchedAssets
    bs_presentImagePickerController(vc, animated: true,
                                    select: { (asset: PHAsset) -> Void in
                                        print("Selected: \(asset)")
    }, deselect: { (asset: PHAsset) -> Void in
        print("Deselected: \(asset)")
    }, cancel: { (assets: [PHAsset]) -> Void in
        print("Cancel: \(assets)")
    }, finish: { (assets: [PHAsset]) -> Void in
        print("Finish: \(assets)")
        print(assets.count)
        for i in 0..<assets.count {
            self.SelectedAssets.append(assets[i])
            print(self.SelectedAssets)
        }
    }, completion: nil)