-1

My Bsimagepicker allows me to select 3 images and places them in a paging UIScrollView but the first two images are missing and the last image shows up. I had it working before something changed. so the image picker should select up to 3 images and put the images in a paging scroll view with a page indicator so you can see what image in the gallery you are looking at. Any help would be much appreciated

enter image description here
enter image description here enter image description here enter image description here

     @objc func uploadImg( sender: UIButton) {
     PhotoArray.removeAll()
     SelectedAssets.removeAll()
     self.fillImg.isHidden = true
     let imagePicker = ImagePickerController()
     imagePicker.settings.selection.max = 3
     imagePicker.settings.theme.selectionStyle = .numbered
     imagePicker.settings.fetch.assets.supportedMediaTypes = [.image]
     imagePicker.settings.selection.unselectOnReachingMax = true

     let start = Date()
     self.presentImagePicker(imagePicker, select: { (asset) in
         print("Selected: \(asset)")
     }, deselect: { (asset) in
         print("Deselected: \(asset)")
     }, cancel: { (assets) in
         print("Canceled with selections: \(assets)")
     }, finish: { (assets) in
         for i in 0..<assets.count
         {
             self.SelectedAssets.append(assets[i])

         }

         self.convertAssetToImages()

         print("Finished with selections: \(assets)")
     }, completion: {
         let finish = Date()
         print(finish.timeIntervalSince(start))


     })

     }

func convertAssetToImages() -> Void {

       if SelectedAssets.count != 0{


           for i in 0..<SelectedAssets.count{



               let manager = PHImageManager.default()
               let option = PHImageRequestOptions()
               var thumbnail = UIImage()
               option.isSynchronous = true


               manager.requestImage(for: SelectedAssets[i], targetSize: CGSize(width: 1000, height: 1000), contentMode: .aspectFill, options: option, resultHandler: {(result, info)->Void in
                   thumbnail = result!

               })


            let data = thumbnail.jpegData(compressionQuality: 0.7)
            let newImage = UIImage(data: data!)


            self.PhotoArray.append(newImage! as UIImage)


            scrollImg.image = PhotoArray[i]
            scrollImg.contentMode = .scaleAspectFill
            let xPosition = scrollView.frame.width * CGFloat(i)
            scrollImg.clipsToBounds = true
            scrollImg.frame = CGRect(x: xPosition, y: 0, width: scrollView.frame.width , height: scrollView.frame.height)
            scrollView.contentSize.width = scrollView.frame.width * CGFloat(PhotoArray.count)
            self.pageC.numberOfPages = PhotoArray.count
            scrollView.addSubview(scrollImg)
                           }


    }
       }
NicolasElPapu
  • 1,612
  • 2
  • 11
  • 26
Colangelo
  • 43
  • 9

1 Answers1

1

The scrollImg is the same UIImageView instance on each interaction, that's why only the last image is shows up, to avoid this you have to instantiate the scrollImg every single interaction, here are two options:

scrollImg = UIImageView(frame: CGRect(x: xPosition, y: 0, width: scrollView.frame.width , height: scrollView.frame.height)
scrollImg.image = PhotoArray[i]
scrollImg.contentMode = .scaleAspectFill
let xPosition = scrollView.frame.width * CGFloat(i)
scrollImg.clipsToBounds = true
scrollView.contentSize.width = scrollView.frame.width * CGFloat(PhotoArray.count)
self.pageC.numberOfPages = PhotoArray.count
scrollView.addSubview(scrollImg)

or

func setupSlideScrollView(_ photoArray: [UIImage]) {
    scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(photoArray.count), height: scrollView.frame.height)
    scrollView.isPagingEnabled = true

    for (i, img) in photoArray.enumerated() {
        let imageView = UIImageView(frame:
            CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: scrollView.frame.height)
        )
        imageView.image = img
        imageView.contentMode = .scaleAspectFill
        scrollView.addSubview(imageView)
    }
}
Allan
  • 51
  • 4