0

Do consider this as a question from someone who is not so good at swift..:).I have a button on the click of which the imagepicker is opened and I am able to select the images. In the didFinishPickingMediaWithInfo I'm adding the image to array like so...

    var imageArray = [UIImage]()


    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
        imageArray.append(image)

   for i in 0..<imageArray.count {

            imageView.image = imageArray[i]
            imageView.contentMode = .scaleAspectFit
            let xPosition = self.view.frame.width * CGFloat(i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)

            imageScrollView.contentSize.width = imageScrollView.frame.width * (CGFloat(i + 1))
            imageScrollView.addSubview(imageView)
        }

     }
      self.dismiss(animated: true, completion: nil)
    }

I'm also having these functions:

func saveImage(image: UIImage, path: String) -> Bool {
    let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
    do {
        try jpgImageData?.write(to: URL(fileURLWithPath: path), options: .atomic)
    } catch {
        print(error)
    }
    return (jpgImageData != nil)
}

func getDocumentsURL() -> NSURL {
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    return documentsURL as NSURL
}

func fileInDocumentsDirectory(filename: String) -> String {
    let fileURL = getDocumentsURL().appendingPathComponent(filename)
    return fileURL!.path
}

But my issue is this..I just don't want to show just one image that is picked from the gallery. I want to pick multiple images from the gallery(one at a time), store them in an array and then display them all in a horizontal scrolling format. For this purpose, I'm setting a scrollview to take the images(as given in didFinishPickingMediaWithInfo)

Maybe I have to read the image also. But how that can be done I'm not able to figure out...Please help!

  • you can refer this link https://stackoverflow.com/questions/20756899/how-to-select-multiple-images-from-uiimagepickercontroller – DHEERAJ Oct 03 '17 at 09:40
  • @DHEERAJ The libraries in swift don't seem to support swift 3...:( –  Oct 03 '17 at 10:23
  • Use one of the libraries https://stackoverflow.com/a/20756957/1088836 to pick more than one image per time and use CollectionView to present them horizontally. – mbelsky Oct 03 '17 at 11:27
  • mbelsky thats the very link @DHEERAJ suggested...:) –  Oct 03 '17 at 11:29

2 Answers2

0

Use this to save image

var imageArr:[UIImage] = []
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
            UIImageWriteToSavedPhotosAlbum(chosenImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
            imageArr.append(chosenImage)

            for i in 0..<imageArray.count 
            {
                imageView.image = imageArray[i]
                imageView.contentMode = .scaleAspectFit
                let xPosition = self.view.frame.width * CGFloat(i)
                imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)
                imageScrollView.contentSize.width = imageScrollView.frame.width * (CGFloat(i + 1)) 
                imageScrollView.addSubview(imageView)
            }

     }
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
  • ..actually I wanted to save the images to imageArray and then show the images in a horizontal scrolling format.. –  Oct 03 '17 at 10:21
  • your answer does save the image to array. But after that I'm adding the images to a scrollview so as to display them them in a horizontal scrolling manner. I have updated that in the first code snippet above. But the issue is if I select 3 images the array shows the count as 3 but in the scrollview only 1 image (the latest one) is displayed. –  Oct 03 '17 at 11:19
  • please debug your hierarchy view and screen shot here with clip to bound disable, change your code to old one – Varun Naharia Oct 03 '17 at 12:42
0

Please see this loop which i have corrected

You are only creating one UIImageView and adding to the scrollview.

Please initialize the UIImageView every time

   for i in 0..<imageArray.count {
            var imageView = UIImageView()        //*** Add this line to your code
            imageView.image = imageArray[i]
            imageView.contentMode = .scaleAspectFit
            let xPosition = self.view.frame.width * CGFloat(i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)

            imageScrollView.contentSize.width = imageScrollView.frame.width * (CGFloat(i + 1))
            imageScrollView.addSubview(imageView)
        }

When ever you update your scrollview with newImages dont forget to remove the old ones.

  • multiple images do get added now! But when more than one image is added, there is too much spacing between both images..more than the normal spacing...:) Can we reduce the spacing between the images by any means...? –  Oct 03 '17 at 12:04