简体   繁体   中英

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

Here i have used third party library BSImagePicker to do multiple selection in our gallery .After selecting the image i cant be able to save and display in the image view .I want the image to be save and display in the collection view .I have imported photos and BSImagePicker.

If i click showImagepicker the multiple images can be selected and it should place in the respective position .I have used 4 image view to set the images which we have selected . Here is my code :

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
}

Here is the custom view class for collectionviewcell:

class CollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imagecollection: UIImageView!

@IBOutlet weak var imageview2: UIImageView!

@IBOutlet weak var imageview3: UIImageView!

@IBOutlet weak var imageview4: UIImageView!

}

I have used 4 outlet of the image view.I need to save in this imageview.

I got an solution for this .

Declare it globally

var Select = PHAsset

var arrimg = UIImage

@IBAction func showImagePicker(_ sender: 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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM