2017-06-05 57 views
0

大家好我正在尝试创建学习。我有一个我正在使用的演示代码,并且想知道是否有更改它显示的图像(硬写入)(汽车名称)的方式并更改它,以便从url中显示图像。因为我正在学习这个东西,我试图学习,并且可以获得如何为其他东西做的例子,但似乎很难将它带入此代码。我希望你能帮忙。从互联网上获取图像到一个集合

我有一个类调用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

参考的swift2。 2:-https://stackoverflow.com/questions/38889134/swift-download-image-from-internet-and-cache-them-doesnt-work-properly-need-s/38890731#38890731 –

回答

1

可以使用数据的指定构造下载从URL的图片:let imageData = Data(contentsOf: "exampleURL.com")

然后你只需在您的收藏查看数据源的方法使用:

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) 
} 
+0

'let imageData = Data (contentsOf:“exampleURL.com”)'??? –

+0

为你的答案干杯@DávidPásztor,我添加了这个,但在图像数据上出现一个错误,让图像数据显示“Call can throw,but it is not'try'and the error is not句柄” –

+0

@LeoDabus my bad,忘记修改该行,纠正它。 –