在图库中保存图像并显示的问题

Issue with saving image in gallery and displaying

请将此视为不太擅长 swift..:) 的人提出的问题。我有一个按钮,单击它会打开 imagepicker,我可以select 图片。在 didFinishPickingMediaWithInfo 中,我将图像添加到数组中,就像这样...

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

我也有这些功能:

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
}

但我的问题是..我只是不想只显示从图库中挑选的一张图片。我想从图库中选择多张图片(一次一张),将它们存储在一个数组中,然后以水平滚动格式显示它们。为此,我设置了一个 scrollview 来拍摄图像(如 didFinishPickingMediaWithInfo 中给出)

也许我还需要阅读图片。但是我不知道该怎么做...请帮忙!

用这个保存图片

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

     }

请查看我已更正的循环

您只是创建一个 UIImageView 并添加到滚动视图。

请每次初始化UIImageView

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

当你用新图片更新你的滚动视图时,不要忘记删除旧的。