Hallo alles, was ich lerne, indem ich versuche zu schaffen. Ich habe einen Demo-Code, mit dem ich arbeite und wollte wissen, ob es eine Möglichkeit gibt, das angezeigte Bild (hart geschrieben) (Namen der Autos) zu ändern und es so zu ändern, dass stattdessen ein Bild von einer URL angezeigt wird. weil ich newt dieses Zeug bin ich versuche zu lernen und kann Beispiele dafür, wie es für andere Dinge zu tun, aber scheint es schwer zu bekommen, es in diesen Code zu bekommen. Ich hoffe du kannst helfen.Holen Sie Bilder aus dem Internet in eine Sammlung
Ich habe eine Klasse Anruf Post und ich bekomme Bilder von Firebase. Also verwende ich: post.imageUrl1 für die Bild-URL.
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)
}
}
Referenz. 2: -https: //stackoverflow.com/questions/38889134/swift-download-image-from-internet-and-cache-them-doesnt-work-properly-need-s/38890731#38890731 –