簡體   English   中英

如何在收藏夾視圖中顯示從圖庫中選擇的多個圖像?

[英]How to display multiple images selected from gallery in collection view?

在這里,我使用了第三方庫BSImagePicker在我們的庫中進行了多次選擇。選擇圖像后,我將無法保存並在圖像視圖中顯示。我希望將圖像保存並在集合視圖中顯示。我已導入照片和BSImagePicker。

如果我單擊showImagepicker,則可以選擇多個圖像,並且應該將其放置在各自的位置。我使用4圖像視圖來設置我們選擇的圖像。 這是我的代碼:

class ViewController: UIViewController ,UICollectionViewDataSource,UICollectionViewDelegate{

@IBAction func showImagePicker(_ sender: UIButton) {
    let vc = BSImagePickerViewController()
    vc.maxNumberOfSelections = 4
    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)")
        if let imageView = vc.imageview{
            PHCachingImageManager.default().requestImage(for: asset, targetSize:imageView.frame.size, contentMode: .aspectFit, options: options) { (result, _) in
                imageView.image = result
            }
        }
    }, completion: nil)
}

 @IBOutlet weak var collectionview: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    cell.imagecollection.image = imageview[indexPath.row]
    return cell
}

這是collectionviewcell的自定義視圖類:

class CollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imagecollection: UIImageView!

@IBOutlet weak var imageview2: UIImageView!

@IBOutlet weak var imageview3: UIImageView!

@IBOutlet weak var imageview4: UIImageView!

}

我已經使用了4個圖像視圖的出口。我需要保存在此圖像視圖中。

我為此找到了解決方案。

在全球范圍內聲明

var Select = PHAsset

var arrimg = UIImage

@IBAction func showImagePicker(_發件人:UIButton){

    let imgPkr = BSImagePickerViewController()

    self.bs_presentImagePickerController(imgPkr, animated: true, select: {(asset : PHAsset) -> Void in }, deselect: {(asset : PHAsset) -> Void in}, cancel: {(assets : [PHAsset]) -> Void in}, finish: {(assets : [PHAsset]) -> Void in

        for i in 0..<assets.count
        {
            self.Select.append(assets[i])

        }
    }, completion: nil)}

@objc func getAllImg() -> Void
{

    if Select.count != 0{
        for i in 0..<Select.count{
            let manager = PHImageManager.default()
            let option = PHImageRequestOptions()
            var thumbnail = UIImage()
            option.isSynchronous = true
            manager.requestImage(for: Select[i], targetSize: CGSize(width: 300, height: 300), contentMode: .aspectFill, options: option, resultHandler: {(result, info)->Void in
                thumbnail = result!
            })
            self.arrimg.append(thumbnail) 
        }
    }
    collectionview.reloadData()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM