2016-09-06 58 views
0

Im努力從Firebase存儲中將圖像檢索到TableView。感謝任何幫助。下面是我的代碼:在Swift Table中從Firebase存儲中恢復圖像

tableviewcell代碼:

class HomeFeedCell : UITableViewCell { 

    Var feeds = [HomeFeed]() 
    var dbRef :FIRDatabaseReference! 
    var storage : FIRStorage! 


    @IBOutlet weak var Name: UILabel! 
    @IBOutlet weak var Location: UILabel! 
    @IBOutlet weak var Type: UILabel! 

    @IBOutlet weak var FeedImage: UIImageView! 

    func configureCellWith(product : HomeFeed) 
    { 
     Name.text? = product.name 
     Location.text? = product.location 
     Type.text? = product.type 

     func setSelected(selected: Bool, animated: Bool) { 
      super.setSelected(selected, animated: animated) 
     } 

     // Configure the view for the selected state 
    } 

} 

我的實現代碼如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! HomeFeedCell 

    let details = feeds[indexPath.row] 

    cell.configureCellWith(details) 

    return cell 
} 

我能夠成功顯示的名稱,位置和類型的詳細信息,但我只是停留在檢索圖像上。下面是我的結構文件:

struct HomeFeed { 

    let key:String! 
    let name:String! 
    let location:String! 
    let review:String! 
    let type:String! 
    let photoURL:String 

回答

1

這可能是因爲你會想要做這樣的事情:(基於傳遞到細胞的名字,或其他一些

下載您的表視圖單元格唯一標識符):

// Create a storage reference from the URL 
    let storageRef = storage.reference("name/of/my/object/to/download.jpg") 
    // Download the data, assuming a max size of 1MB (you can change this as necessary) 
    storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in 
    // Create a UIImage, add it to the array 
    let pic = UIImage(data: data) 
    FeedImage.image = pic 
    }) 

欲瞭解更多信息,請參閱Zero to App: Develop with Firebase,它的associated source code,對於如何做到這一點的實際例子。

0

有兩種方法可以從網址讓你的形象,異步和同步,這兩個我都在下面的使用說明

同步方式:異步

if let filePath = Bundle.main.pathForResource("imageName", ofType: "jpg"), image = UIImage(contentsOfFile: filePath) 
{ 
    imageView.contentMode = .scaleAspectFit 
    imageView.image = image 
} 

完成處理程序創建一個方法來從你的網址,以獲取圖像數據

func getDataFromUrl(url: URL, completion: ((data: Data?, response: URLResponse?, error: NSError?) -> Void)) { 
URLSession.shared.dataTask(with: url) { 
    (data, response, error) in 
    completion(data: data, response: response, error: error) 
}.resume() 
} 

創建下載圖像的方法(啓動任務)

func downloadImage(url: URL){ 
print("Download Started") 
getDataFromUrl(url: url) { (data, response, error) in 
    guard let data = data where error == nil else { return } 
    DispatchQueue.main.async() {() -> Void in 
     print(response?.suggestedFilename ?? url.lastPathComponent ?? "") 
     print("Download Finished") 
     self.imageView.image = UIImage(data: data) 
    } 
} 
} 

用法:

override func viewDidLoad() { 
super.viewDidLoad() 
print("Begin of code") 
if let checkedUrl = URL(string: "http://www.apple.com/euro/ios/ios8/a/generic/images/og.png") { 
    imageView.contentMode = .scaleAspectFit 
    downloadImage(url: checkedUrl) 
} 
print("End of code. The image will continue downloading in the background and it will be loaded when finished.") 
} 

希望這會幫助你!