我的 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)
}
}
}
所以... 滚动图像 是同一个UIImageView实例,这就是为什么只有最后一张图片会显示出来的原因,为了避免这种情况,你必须将 滚动图像 每一次互动,这里有两个选择。
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)
}
}