tom tom - 1 month ago 16
iOS Question

pass an image from one view controller to another (swift) with Firebase

i am trying to pass an image from one view controller to another but i am getting an error in prepare for segue function

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()
}
}


this is my Post class

class Post {

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 and imageURL are saved on firebase database

Answer

Can you try with following change in preparefor segue method.. let me know

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    {

         if segue.identifier == "showImage"
         {

         let indexPaths = self.collectionView!.indexPathsForSelectedItems!
         let indexPath = indexPaths[0] as IndexPath
         let vc = segue.destinationViewController as! newViewController

         let post = posts[indexPath.row]
         if let img = booksVC.imageCache.object(forKey: post.imageUrl as NSString) {
                vc.image = img
         }
       }
    }


class newViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    var image = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        if self.image {
         self.imageView.image = self.image
        }
    }

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