こんにちは、私は作成しようとして学んでいます。私は作業中のデモコードを持っていて、表示されている画像(ハードの名前)(車の名前)を変更し、代わりにURLから画像を表示するように変更する方法があるかどうかを知りたがっています。私はこのことを勉強しているし、他のもののためにそれをする方法の例を得ることができますが、このコードにそれを得るのに苦労しているようです。私はあなたが助けることを願っていますインターネットから収集した画像を収集する
私はクラスコールポストを持っており、私はfirebaseから画像を取得しています。だから私は使用:画像のURLの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)
}
}
リファレンス。 2:-https://stackoverflow.com/questions/38889134/swift-download-image-from-internet-and-cache-them-doesnt-work-properly-need-s/38890731#38890731 –