嗨,我正在通过尝试创作来学习。我有一个我正在使用的演示代码,想要知道是否有一种方法可以改变它显示的图像(硬写入)(汽车的名称)并更改它,以便它显示来自网址的图像。因为我正在尝试学习这些东西,并且可以获得如何为其他事情做这些事情的例子,但似乎很难将其纳入此代码。我希望你能提供帮助。
我有一个课程调用Post,我从firebase获取图像。所以我使用:post.imageUrl1作为图片网址。
class DemoViewController: ExpandingViewController {
@IBAction func backPressed(_ sender: Any) {
}
var post: Post!
typealias ItemInfo = (imageName: String, title: String)
fileprivate var cellsIsOpen = [Bool]()
fileprivate let items: [ItemInfo] = [("Ferrari", "Ferrari"),("Bug", "Bugatti"),("car", "Mustang"),("BM", "BMW")]
@IBOutlet weak var pageLabel: UILabel!
@IBOutlet weak var titleImageView: UIImageView!
@IBOutlet weak var titleImageViewXConstraint: NSLayoutConstraint!
}
// MARK: - Lifecycle
extension DemoViewController {
override func viewDidLoad() {
itemSize = CGSize(width: 256, height: 335)
super.viewDidLoad()
registerCell()
fillCellIsOpenArray()
addGesture(to: collectionView!)
configureNavBar()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
guard let titleView = navigationItem.titleView else { return }
let center = UIScreen.main.bounds.midX
let diff = center - titleView.frame.midX
titleImageViewXConstraint.constant = diff
}
}
// MARK: Helpers
extension DemoViewController {
fileprivate func registerCell() {
let nib = UINib(nibName: String(describing: DemoCollectionViewCell.self), bundle: nil)
collectionView?.register(nib, forCellWithReuseIdentifier: String(describing: DemoCollectionViewCell.self))
}
fileprivate func fillCellIsOpenArray() {
cellsIsOpen = Array(repeating: false, count: items.count)
}
fileprivate func getViewController() -> ExpandingTableViewController {
let storyboard = UIStoryboard(storyboard: .Main)
let toViewController: DemoTableViewController = storyboard.instantiateViewController()
return toViewController
}
fileprivate func configureNavBar() {
navigationItem.leftBarButtonItem?.image = navigationItem.leftBarButtonItem?.image!.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
}
/// MARK: Gesture
extension DemoViewController {
fileprivate func addGesture(to view: UIView) {
let upGesture = Init(UISwipeGestureRecognizer(target: self, action: #selector(DemoViewController.swipeHandler(_:)))) {
$0.direction = .up
}
let downGesture = Init(UISwipeGestureRecognizer(target: self, action: #selector(DemoViewController.swipeHandler(_:)))) {
$0.direction = .down
}
view.addGestureRecognizer(upGesture)
view.addGestureRecognizer(downGesture)
}
func swipeHandler(_ sender: UISwipeGestureRecognizer) {
let indexPath = IndexPath(row: currentIndex, section: 0)
guard let cell = collectionView?.cellForItem(at: indexPath) as? DemoCollectionViewCell else { return }
// double swipe Up transition
if cell.isOpened == true && sender.direction == .up {
pushToViewController(getViewController())
if let rightButton = navigationItem.rightBarButtonItem as? AnimatingBarButton {
rightButton.animationSelected(true)
}
}
let open = sender.direction == .up ? true : false
cell.cellIsOpen(open)
cellsIsOpen[indexPath.row] = cell.isOpened
}
}
// MARK: UIScrollViewDelegate
extension DemoViewController {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageLabel.text = "\(currentIndex+1)/\(items.count)"
}
}
// MARK: UICollectionViewDataSource
extension DemoViewController {
override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
super.collectionView(collectionView, willDisplay: cell, forItemAt: indexPath)
guard let cell = cell as? DemoCollectionViewCell else { return }
let index = indexPath.row % items.count
let info = items[index]
cell.backgroundImageView?.image = UIImage(named: info.imageName)
cell.customTitle.text = info.title
cell.cellIsOpen(cellsIsOpen[index], animated: false)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) as? DemoCollectionViewCell
, currentIndex == indexPath.row else { return }
if cell.isOpened == false {
cell.cellIsOpen(true)
} else {
pushToViewController(getViewController())
if let rightButton = navigationItem.rightBarButtonItem as? AnimatingBarButton {
rightButton.animationSelected(true)
}
}
}
}
// MARK: UICollectionViewDataSource
extension DemoViewController {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: DemoCollectionViewCell.self), for: indexPath)
}
}
答案 0 :(得分:1)
您可以使用数据的指定构造函数从网址下载图片:let imageData = Data(contentsOf: "exampleURL.com")
然后您只需在Collection View数据源方法中使用它:
override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
super.collectionView(collectionView, willDisplay: cell, forItemAt: indexPath)
guard let cell = cell as? DemoCollectionViewCell else { return }
let index = indexPath.row % items.count
let info = items[index]
//here I assumed you would create a .url property of ItemInfo, but you can access the URL any way you want to
guard let imageUrl = URL(string: info.url) else { return }
do {
let imageData = try Data(contentsOf: imageUrl)
} catch {
//handle error here
}
cell.backgroundImageView?.image = UIImage(data: imageData)
cell.customTitle.text = info.title
cell.cellIsOpen(cellsIsOpen[index], animated: false)
}