gpt4 book ai didi

ios - 使用 Firebase 将图像从一个 View Controller 传递到另一个 View Controller (快速)

转载 作者:行者123 更新时间:2023-11-29 00:37:22 24 4
gpt4 key购买 nike

我正在尝试将图像从一个 View Controller 传递到另一个 View Controller ,但在准备 segue 函数时出现错误

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


let post = posts[indexPath.row]

if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as? collectionViewCellBooks {

if let img = booksVC.imageCache.object(forKey: post.imageUrl as NSString) {
cell.configureCell(post: post, img: img)
return cell

}else {

cell.configureCell(post: post)
return cell
}
}
else {
return collectionViewCellBooks()
}

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showImage", sender: self)
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{

if segue.identifier == "showImage"
{

let indexPaths = self.collectionView!.indexPathsForSelectedItems!
let indexPath = indexPaths[0] as IndexPath
let vc = segue.destination as! newViewController
// vc.image = self.posts[(indexPath as NSIndexPath).row]
vc.image = self.posts[indexPath.row]
}





class newViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

var image = UIImage()

override func viewDidLoad() {
super.viewDidLoad()

self.imageView.image = self.image
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

这是我的帖子类(class)

类(class)帖子{

private var _caption: String!
private var _imageUrl: String!
private var _postKey: String!



var caption: String {
return _caption
}

var imageUrl: String {
return _imageUrl
}

var postKey: String {
return _postKey
}


init(caption: String, imageUrl: String) {

self._caption = caption
self._imageUrl = imageUrl
}

init(postKey: String, postData: Dictionary<String, AnyObject>) {

self._postKey = postKey

if let caption = postData["title"] as? String {
self._caption = caption
}
if let imagesUrl = postData["imageURL"] as? String {
self._imageUrl = imagesUrl
}

}

title 和 imageURL 保存在 firebase 数据库中

最佳答案

您收到错误消息是因为您没有将图像发送到新的 viewController,而是发送了 Post 类的一个实例,顺便说一句,它甚至不包含图像(只有 imageURL)。您必须先从服务器中提取图像,然后才能在任何地方解析它。

您应该像现在一样解析整个帖子,并直接在新的 ViewController 中通过 postID 下载图像。 (如果您将图像保存在 Firebase 存储中)我总是最终解析整个对象,因为在开发的后期您可能决定在 newViewController 中显示更多属性。如果您解析了整个对象,则无需再更改代码结构。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "your Identifier" {
if let vc = segue.destination as? NewViewController {
if let post = sender as? Post {
vc.post = post
}
}
}
}

在您的 didSelectIdemAt 函数中,您需要更改 performSegue 函数的发送者:

 performSegue(withIdentifier: "your Identifier", sender: post)

现在你的 newViewController 有更多的代码,但我就是这样做的,它工作稳定。

let reference = FIRDatabase.database().reference()
reference.child("posts").child("<postID>").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
print(snapshot.value)

if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {

if let postsDict = snap.value as? Dictionary<String, AnyObject> {

if let imageURL = postsDict["imageURL"] as? String {
let httpsReference = FIRStorage.storage().reference(forURL: imageURL)
httpsReference.data(withMaxSize: 1 * 1024 * 1024, completion: { (data, error) in
if error != nil {
print("error... couldn't get picture from Server \(error.localizedDescription)")
} else {
let image = UIImage(data: data!)
self.img = image! // you need to create this variable somewhere in your viewController Class before this code
//"your UIImageVIew.image" = img AND THAT IS IT
}
}
}
}
}
}

关于ios - 使用 Firebase 将图像从一个 View Controller 传递到另一个 View Controller (快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40299122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com