我正在尝试在两个视图控制器之间传输图像。它是当前已登录用户的个人资料图片(如果有)。该映像已成功从Firebase下载。它与用户名一起下载到启动屏幕上。如果没有用户登录,则视图控制器将切换到登录屏幕,如果有已登录的用户,则视图控制器将转移到主屏幕。 这是启动屏幕。用户名标签和图像视图都清晰可见,因此用户看不到。
import UIKit
import Firebase
class LaunchScreen: UIViewController {
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var userImage: UIImageView!
var ref : DatabaseReference!
var storage: StorageReference!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
storage = Storage.storage().reference()
checkUser()
// Do any additional setup after loading the view.
}
func checkUser() {
Auth.auth().addStateDidChangeListener { (auth, user) in
if user != nil {
let uid = Auth.auth().currentUser?.uid
self.ref.database.reference().child("users/profile").child(uid!).observe(.value, with: { (snapshot: DataSnapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let currentUser = dictionary["username"] as! String
let url = dictionary["photoURL"] as! String
let storageRef = self.storage.storage.reference(forURL: url)
storageRef.getData(maxSize: 1 * 1024 * 1024, completion: { (data, error) in
if let error = error {
print(error)
} else {
self.userImage.image = UIImage(data: data!)
self.reloadInputViews()
}
})
guard let image = self.userImage.image else {return}
self.usernameLabel.text = currentUser
let imageJPG = image.jpegData(compressionQuality: 1)
print(imageJPG?.count)
let defaults = UserDefaults.standard
defaults.set(self.usernameLabel.text, forKey: "user")
defaults.synchronize()
}
})
_ = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false, block: { (timer) in
let vc = self.storyboard?.instantiateViewController(withIdentifier: "homeVC")
self.present(vc!, animated: true, completion: nil)
})
print("loggedIn")
} else {
_ = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false, block: { (timer) in
let loginvc = self.storyboard?.instantiateViewController(withIdentifier: "loginVC")
self.present( loginvc!,animated: true, completion: nil)
})
print("loggedOut")
}
}
}
}
这是我的主页。那里有一个侧面菜单。左侧菜单从屏幕左侧弹出,上面有四个收藏视图单元格。最上面的是配置文件单元格,它将显示用户名和用户配置文件图像。该图像也是我也要传输在launchScreenVC中下载的Firebase userImage的位置。
class home VC: UIViewController {
@IBOutlet weak var profileMenuButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
let blackView = UIView()
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero , collectionViewLayout: layout )
cv.backgroundColor = UIColor.init(displayP3Red: 50/255, green: 51/255, blue: 51/255, alpha: 1)
return cv
}()
@IBAction func profileMenu() {
let width = view.frame.width - 60
collectionView.frame = CGRect(x: 0, y: 0, width: width, height: view.frame.height)
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
blackView.frame = view.frame
blackView.alpha = 0
view.addSubview(blackView)
view.addSubview(collectionView)
UIView.animate(withDuration: 1.5, delay: 0, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = CGRect(x: 0 , y: 0, width: width, height: self.view.frame.height)
}, completion: nil)
handleSearchDismiss(searchView)
}
@objc func handleDismiss() {
UIView.animate(withDuration: 0.5){
if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = CGRect(x: 0 - (window.frame.width), y: 0, width: window.frame.width, height: self.collectionView.frame.height)
}
self.blackView.alpha = 0
}
}
}
extension homeVC: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileCellID", for: indexPath) as! profileCell
let defaults = UserDefaults.standard
cell1.usernameLabel.text = (defaults.object(forKey: "user") as! String)
cell1.usernameLabel.textColor = UIColor.lightGray
cell1.usernameLabel.font = UIFont.boldSystemFont(ofSize: 25)
// print(imageData)
// cell1.profileImage.image = (defaults.object(forKey: "userImage") as! Data)
return cell1
} else if indexPath.item == 1{
let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "Settings", for: indexPath) as! settingsCell
return cell2
} else if indexPath.item == 2 {
let cell3 = collectionView.dequeueReusableCell(withReuseIdentifier: "Favourites", for: indexPath) as! favouritesCell
return cell3
} else {
let cell4 = collectionView.dequeueReusableCell(withReuseIdentifier: "Logout", for: indexPath) as! logoutCell
return cell4
}
}
}
这是个人资料单元格类
class profileCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setUpViews()
}
let profileImage: UIImageView = {
let imageView = UIImageView()
return imageView
}()
let usernameLabel: UILabel = {
let label = UILabel()
return label
}()
func setUpViews() {
addSubview(profileImage)
addSubview(usernameLabel)
profileImage.frame = CGRect(x: (center.x)-60, y: 30, width: 120, height: 120)
profileImage.layer.cornerRadius = (profileImage.frame.width/2)
usernameLabel.frame = CGRect(x: (center.x)-60, y: 125, width: 120, height: 120)
usernameLabel.textAlignment = NSTextAlignment.center
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
任何帮助或提示将不胜感激!谢谢你们!