0

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.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • how will you created the image array `imageview` – Anbu.Karthik Sep 27 '18 at 10:13
  • I just checked imageview out of the collection view and tried to set whether the single image is saving or not .i have created the outlet for that and i have deleted that outlet . – Bhuvaneswari Lakshminarayanan Sep 27 '18 at 10:16
  • But my scenario is i want to save and display in collection view only .If i select 4 images from the gallery ,It should display in the collection view.In my case i can select the multiple images but i do no where to save and fetch again to display in collection view – Bhuvaneswari Lakshminarayanan Sep 27 '18 at 10:19
  • Simple process.1. Create an array of UIImage as var imageArray :[UIImage] = () 2. add selected images in imageArray. 3. Save image from imageArray in Document Directory as https://stackoverflow.com/questions/32836862/how-to-use-writetofile-to-save-image-in-document-directory. You can use a Background process to save images. 4. Now display images your collectionView with the help of imageArray. – Gagan_iOS Sep 27 '18 at 10:24
  • you get the image from gallery in array mode `finish: { (assets: [PHAsset])`, so you need to iterate the [PHAsset] using for loop and add each `imageView.image = result` to array then you can easily show in collectionview – Anbu.Karthik Sep 27 '18 at 10:25

1 Answers1

0

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