ios - 使用 DKImagePickerController 在 ScrollView 上显示多个图像

标签 ios swift xcode uiscrollview

当用户使用 DKImagePickerController github 选择图像时,我想在 ScrollView 上显示多个图像。 这是我的代码,但图像没有出现。任何人都可以知道出了什么问题吗? 预先感谢您!

    var picker = DKImagePickerController()
    var imagesArray = [Any]()

     @IBAction func pickPhoto(_ sender: Any) {

            picker.maxSelectableCount = 10
            picker.showsCancelButton = true
            picker.allowsLandscape = false
            picker.assetType = .allPhotos

            self.present(picker, animated: true)

            picker.didSelectAssets = { (assets: [DKAsset]) in

                self.imagesArray.append(assets)

                for i in 0..<self.imagesArray.count {

                    let imageView = UIImageView()
                    imageView.image = self.imagesArray[i] as? UIImage
                    imageView.contentMode = .scaleAspectFit
                    let xposition = self.view.frame.width * CGFloat(i)
                    imageView.frame = CGRect(x: xposition, y: 330, width: self.scrollView.frame.width, height: 170)
                    self.scrollView.contentSize.width = self.scrollView.frame.width * CGFloat(i * 1)
                    self.scrollView.addSubview(imageView)
                }

            }

        }

最佳答案

您可以将图像添加到数组中并重新加载collectionView/TableView,而不是将图像显示到scrollView。

正如我在 collectionView 中所做的那样

func showImagePicker() {
    let pickerController = DKImagePickerController()
    self.previewView?.isHidden = false
    pickerController.assetType = .allPhotos
    pickerController.defaultAssetGroup = .smartAlbumUserLibrary
    pickerController.didSelectAssets = { (assets: [DKAsset]) in
        if  assets.count>0
        {
            for var i in 0 ... assets.count-1 {
                assets[i].fetchOriginalImage(true, completeBlock: { (image, info) in
                        self.abc.append(image)
                })
            }
        }
        self.previewView?.reloadData()
    }
    self.present(pickerController, animated: true) {}
}

这里 abc :[UIImage] 和previewView? : UICollectionView 并在集合委托(delegate)方法中:

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return self.abc.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell: UICollectionViewCell?
    var imageView: UIImageView?

    cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellImage", for: indexPath)
    imageView = cell?.contentView.viewWithTag(1) as? UIImageView
    if let cell = cell, let imageView = imageView
    {
        let tag = indexPath.row + 1
        cell.tag = tag
        imageView.image = self.abc[indexPath.row]
    }
    return cell!
}

关于ios - 使用 DKImagePickerController 在 ScrollView 上显示多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648892/

相关文章:

ios - 从 php/mysql 向 iPhone 发送消息

ios - Swift 错误无法转换类型 '(AFHTTPRequestOperation?, AnyObject?) 的值 -> ()

ios - Swift 3 中的 UIPinchGestureRecognizer

html - 将 HTML 表格抓取到 UITableView

ios - 使用定义的协议(protocol)将 UINavigationController 功能添加到容器 Controller

ios - 在 viewDidLoad 中模拟按钮点击

ios - 如何在 React Native 中构造 POST 请求主体,而不是使用字符串化的 json,而是使用 json?

ios - 添加多个 NSDecimalNumbers

iOS 其他应用中的点击/拖动事件

iOS 应用具有神秘的 UUID,没有匹配的 dSYM,因此无法符号化