Search code examples
iosswiftuiimagepickercontroller

Issue with saving image in gallery and displaying


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!


Solution

  • 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.