-1

Bsimagepicker允许我选择 3 个图像并将它们放在一个页面中UIScrollView,但前两个图像丢失并且最后一个图像显示。在某些事情发生变化之前,我已经让它工作了。所以图像选择器应该选择最多 3 张图像并将图像放在带有页面指示器的分页滚动视图中,这样您就可以看到您正在查看的图库中的图像。任何帮助将非常感激

在此处输入图像描述
在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

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


    }
       }
4

1 回答 1

1

scrollImg在每次交互都是相同的 UIImageView 实例,这就是为什么只显示最后一个图像的原因,为避免这种情况,您必须在每次交互时实例化scrollImg ,这里有两个选项:

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)

或者

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)
    }
}
于 2020-05-10T15:09:54.800 回答