首页 > 解决方案 > UIImage 没有出现在 UIScrollView 中

问题描述

我使用名为 BSImagePicker 的可可豆荚从图书馆抓取照片。然后将图像存储到 photoArray 中。我想从数组中获取这些图像并将它们呈现在滚动视图中的图像视图中。然而,图像根本没有出现。

这是我目前尝试过的。

这是 cocoa pod 用来将图像存储到数组中的代码:

func openImagePicker() {
    let vc = BSImagePickerViewController()

    self.bs_presentImagePickerController(
        vc, 
        animated: true,
        select: { (assest: PHAsset) -> Void in },
        deselect: { (assest: PHAsset) -> Void in },
        cancel: { (assest: [PHAsset]) -> Void in },
        finish: { (assest: [PHAsset]) -> Void in
            for i in 0..<assest.count {
                self.SelectedAssets.append(assest[i])
            }

            self.convertAssetToImages()
        }, 
        completion: nil
    )
    print(photoArray)
    setupImages(photoArray)
}

这是将资产转换为图像的代码

extension addImages {

    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

                let width = scrollView.frame.width
                let height = scrollView.frame.height

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

                let data = thumbnail.jpegData(compressionQuality: 1)
                let newImage = UIImage(data: data! as Data)
                self.photoArray.append(newImage! as UIImage)
            }
        } 
    }
}

此代码将照片数组中的图像放入滚动视图

func setupImages(_ images: [UIImage]){
    for a in 0..<photoArray.count {

        let theImage = UIImageView()
        theImage.image = self.photoArray[a]
        let xPosition = UIScreen.main.bounds.width * CGFloat(a)
        self.scrollView.frame = CGRect(x: xPosition, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
        theImage.contentMode = .scaleAspectFit
        self.scrollView.contentSize.width = self.scrollView.frame.width * CGFloat(a + 1)
        self.scrollView.addSubview(theImage)

      }
  }

我不知道为什么它不起作用。

标签: arraysswiftuiscrollviewuiimageviewcocoapods

解决方案


推荐阅读