gpt4 book ai didi

ios - 如何在 Collection View 中显示从图库中选择的多张图像?

转载 作者:行者123 更新时间:2023-11-30 11:07:34 25 4
gpt4 key购买 nike

这里我使用第三方库BSImagePicker在我们的图库中进行多重选择。选择图像后我无法保存并在 ImageView 中显示。我希望图像保存并在 Collection View 中显示。我已经导入了照片和 BSImagePicker。

如果我点击showImagepicker,可以选择多个图像,并且它应该放置在相应的位置。我使用了4个 ImageView 来设置我们选择的图像。这是我的代码:

class ViewController: UIViewController ,UICollectionViewDataSource,UICollectionViewDelegate{

@IBAction func showImagePicker(_ sender: UIButton) {
let vc = BSImagePickerViewController()
vc.maxNumberOfSelections = 4
bs_presentImagePickerController(vc, animated: true,
select: { (asset: PHAsset) -> Void in
print("Selected: \(asset)")
}, deselect: { (asset: PHAsset) -> Void in
print("Deselected: \(asset)")
}, cancel: { (assets: [PHAsset]) -> Void in
print("Cancel: \(assets)")
}, finish: { (assets: [PHAsset]) -> Void in
print("Finish: \(assets)")
if let imageView = vc.imageview{
PHCachingImageManager.default().requestImage(for: asset, targetSize:imageView.frame.size, contentMode: .aspectFit, options: options) { (result, _) in
imageView.image = result
}
}
}, completion: nil)
}

@IBOutlet weak var collectionview: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.imagecollection.image = imageview[indexPath.row]
return cell
}

这是collectionviewcell的自定义 View 类:

class CollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imagecollection: UIImageView!

@IBOutlet weak var imageview2: UIImageView!

@IBOutlet weak var imageview3: UIImageView!

@IBOutlet weak var imageview4: UIImageView!

}

我已经使用了 ImageView 的4个导出。我需要保存在这个 ImageView 中。

最佳答案

我有一个解决方案。

全局声明

var 选择 = PHAsset

var arrimg = UIImage

@IBAction func showImagePicker(_ 发送者: UIButton){

    let imgPkr = BSImagePickerViewController()

self.bs_presentImagePickerController(imgPkr, animated: true, select: {(asset : PHAsset) -> Void in }, deselect: {(asset : PHAsset) -> Void in}, cancel: {(assets : [PHAsset]) -> Void in}, finish: {(assets : [PHAsset]) -> Void in

for i in 0..<assets.count
{
self.Select.append(assets[i])

}
}, completion: nil)}

@objc func getAllImg() -> Void
{

if Select.count != 0{
for i in 0..<Select.count{
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: Select[i], targetSize: CGSize(width: 300, height: 300), contentMode: .aspectFill, options: option, resultHandler: {(result, info)->Void in
thumbnail = result!
})
self.arrimg.append(thumbnail)
}
}
collectionview.reloadData()
}

关于ios - 如何在 Collection View 中显示从图库中选择的多张图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52534498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com