Uitableview 在swift 3中从firebase获取图像
我正在使用Firebase作为我的项目的后端。我需要在tableview中显示Firebase数据库中的文本和图像。它在“我的tableview”中仅显示文本,并在检索图像时显示错误为“无法将“UIImage”类型的值转换为所需的参数类型“String”。请任何人纠正我的错误。我的代码如下: //应用程序代理Uitableview 在swift 3中从firebase获取图像,uitableview,firebase,swift3,firebase-realtime-database,firebase-storage,Uitableview,Firebase,Swift3,Firebase Realtime Database,Firebase Storage,我正在使用Firebase作为我的项目的后端。我需要在tableview中显示Firebase数据库中的文本和图像。它在“我的tableview”中仅显示文本,并在检索图像时显示错误为“无法将“UIImage”类型的值转换为所需的参数类型“String”。请任何人纠正我的错误。我的代码如下: //应用程序代理 import UIKit import CoreData import Firebase import FirebaseDatabase import FirebaseStorage @
import UIKit
import CoreData
import Firebase
import FirebaseDatabase
import FirebaseStorage
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
return true
}
//视图控制器
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var ref: FIRDatabaseReference? = nil
var refHandle: UInt? = nil
var userList = [User]()
@IBOutlet var tblHome: UITableView!
let textCellIdentifier = "TextCell"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tblHome.delegate = self
tblHome.dataSource = self
self.ref = FIRDatabase.database().reference()
print("ref = ",ref)
fetchUsers()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("userList is", userList.count)
return userList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath) as! HomeCell
cell.profname.text = userList[indexPath.row].imageName
if let url = URL.init(string: userList[indexPath.row].imageUrl) { // Error: cannot convert value of type 'UIImage?' to expected argument type 'String'
cell.profImage.image.downloadedFrom(url: url)
}
return cell
}
func fetchUsers() {
refHandle = ref?.child("Users").observe(.childAdded, with: { (snapshot) in
print("refHandle =", self.refHandle)
if let dictionary = snapshot.value as? [String: AnyObject] {
print("dictionary =", dictionary)
let user = User()
user.setValuesForKeys(dictionary)
self.userList.append(user)
print("userlist is", user)
DispatchQueue.main.async(execute: {
self.tblHome.reloadData()
})
}
})
}
}
extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { () -> Void in
self.image = image
}
}.resume()
}
// func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
// guard let url = URL(string: link) else { return }
// downloadedFrom(url: url, contentMode: mode)
// }
}
//家庭手机
import UIKit
class HomeCell: UITableViewCell {
@IBOutlet var profImage: UIImageView!
@IBOutlet var profname: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
//使用者
import UIKit
class User: NSObject {
var imageName: String?
var imageUrl: UIImage?
}
根据文档,获取一个字符串并向其传递一个 在您的
User
类中,imageUrl
属于UIImage?
类型,您正在将其传递到URL构造函数中,即使它需要一个字符串
您可以通过将用户
类更改为以下内容来解决此问题:
class User: NSObject
{
var imageName: String?
var imageUrl: String? // pass this to the URL constructor
var image: UIImage? // this stores the actual image
}
哪一行引发错误?如果让url=url.init(字符串:userList[indexPath.row].imageUrl){//错误:无法将类型为“UIImage?”的值转换为预期的参数类型“string”cell.profImage.image.downloadedFrom(url:url)}视图控制器中的这一行出现错误。我还标记了错误的原因@eshirimaNow,在以下位置获取错误:if let url=url.init(string:userList[indexath.row].imageUrl!){//无法将类型为“UIImage?”的值转换为预期的参数类型“string”cell.profImage.image.downloadedFrom(url:url)}//错误:“UIImage”类型的值没有成员“downloadedFrom”,您是否完全理解您的代码?或者你是从网上资源得到的?我这样问是因为您的实际使用与您的设置在两种情况下都不同,即
字符串
问题和下载自
抱歉,但我不能这样做;远离我的笔记本电脑。改为cell.profImage.downloadedFrom(url:url)
。我真的建议不要从SO获取代码,尤其是如果您不确定正在发生什么或如何将其集成到您的项目中。阅读并理解UIImageViews
以及如何分别使用扩展。我仅从堆栈溢出中获得此解决方案。。所以我用了它。我只是在那方面有问题。它在控制台上打印数据。只是我需要从扩展获取数据。