[英]How to pass an array of images for selected multiple images from gallery and display them on another widget in Flutter
[英]How to display multiple images selected from gallery in collection view?
在这里,我使用了第三方库BSImagePicker在我们的库中进行了多次选择。选择图像后,我将无法保存并在图像视图中显示。我希望将图像保存并在集合视图中显示。我已导入照片和BSImagePicker。
如果我单击showImagepicker,则可以选择多个图像,并且应该将其放置在各自的位置。我使用4图像视图来设置我们选择的图像。 这是我的代码:
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的自定义视图类:
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imagecollection: UIImageView!
@IBOutlet weak var imageview2: UIImageView!
@IBOutlet weak var imageview3: UIImageView!
@IBOutlet weak var imageview4: UIImageView!
}
我已经使用了4个图像视图的出口。我需要保存在此图像视图中。
我为此找到了解决方案。
在全球范围内声明
var Select = 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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.