2

I have 15 box and I add an image on each boxnode as first material. How can I load the images from Firebase and add an Image to each node. I already know how to load images from Firebase, I want to know the best way I can add an image on each 15 nodes, Thanks.

Here's the view controller:

    class newsVC: UIViewController, presentVCProtocol {

    @IBOutlet var scnView: SCNView!

    var newsScene = NewsScene(create: true)

    var screenSize: CGRect!
    var screenWidth: CGFloat!
    var screenHeight: CGFloat!

    var posts = [ArtModel]()
    var post: ArtModel!
    var arts = [ArtModel]()

    static var imageCache: NSCache<NSString, UIImage> = NSCache()

    var type: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        let scnView = self.scnView!
        let scene = newsScene
        scnView.scene = scene
        scnView.autoenablesDefaultLighting = true
        scnView.backgroundColor = UIColor.white

DataService.ds.REF_USERS.child((FIRAuth.auth()?.currentUser?.uid)!).child("arts").observe(.value) { (snapshot: FIRDataSnapshot) in
            self.posts = []
            if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for snap in snapshot {
                    if let postDict = snap.value as? Dictionary<String, AnyObject> {
                        let key = snap.key
                        let post = ArtModel(key: key, artData: postDict)
                        self.posts.insert(post, at: 0)
                    }
                }
            }
        }
    }

    func configureView(_ post: ArtModel, img: UIImage? = nil, imageView: FXImageView? = nil) {
        self.post = post
        if img != nil {
            self.newsScene.setup(image: img!)
            print("IMAGE:\(img)")
        } else {
            let ref = FIRStorage.storage().reference(forURL: post.imgUrl)
            ref.data(withMaxSize:  2 * 1024 * 1024, completion: { (data, error) in
                if error != nil {
                    print("JESS: Unable to download image from Firebase storage")
                    print("Unable to download image: \(error?.localizedDescription)")
                } else {
                    print("JESS: Image downloaded from Firebase storage")
                    if let imgData = data {
                        if let img = UIImage(data: imgData) {
                            self.newsScene.setup(image: img)
                            print("IMAGE:\(img)")
                            ProfileVC.imageCache.setObject(img, forKey: post.imgUrl as NSString)
                        }
                    }
                }
            })
        }
    }
}

Here's the NewsScene:

import SceneKit
import CoreMotion
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase

class NewsScene: SCNScene {

    var geometry = SCNBox()
    var boxnode = SCNNode()
    var art: ArtModel!
    var artImage = UIImage()
    var index = IndexPath()
    var geo = SCNBox()
    var cameranode = SCNNode()


    convenience init(create: Bool) {
        self.init()

        setup(image: artImage)
    }

    func setup(image: UIImage) {

        self.artImage = image
        typealias BoxDims = (width: CGFloat, height: CGFloat,
            length: CGFloat, chamferRadius: CGFloat)

        let box1Dim = BoxDims(CGFloat(0.8), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box2Dim = BoxDims(CGFloat(0.7), CGFloat(0.7), CGFloat(0.10), CGFloat(0.01))
        let box3Dim = BoxDims(CGFloat(0.8), CGFloat(0.6), CGFloat(0.10), CGFloat(0.01))
        let box4Dim = BoxDims(CGFloat(0.8), CGFloat(0.9), CGFloat(0.10), CGFloat(0.01))
        let box5Dim = BoxDims(CGFloat(0.9), CGFloat(1.0), CGFloat(0.10), CGFloat(0.01))
        let box6Dim = BoxDims(CGFloat(0.4), CGFloat(0.5), CGFloat(0.10), CGFloat(0.01))
        let box7Dim = BoxDims(CGFloat(0.9), CGFloat(0.7), CGFloat(0.10), CGFloat(0.01))
        let box8Dim = BoxDims(CGFloat(0.7), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box9Dim = BoxDims(CGFloat(0.9), CGFloat(0.9), CGFloat(0.10), CGFloat(0.01))
        let box10Dim = BoxDims(CGFloat(0.6), CGFloat(0.6), CGFloat(0.10), CGFloat(0.01))
        let box11Dim = BoxDims(CGFloat(0.7), CGFloat(0.7), CGFloat(0.10), CGFloat(0.01))
        let box12Dim = BoxDims(CGFloat(0.8), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box13Dim = BoxDims(CGFloat(0.6), CGFloat(0.8), CGFloat(0.10), CGFloat(0.01))
        let box14Dim = BoxDims(CGFloat(0.6), CGFloat(0.6), CGFloat(0.10), CGFloat(0.01))
        let box15Dim = BoxDims(CGFloat(0.9), CGFloat(0.9), CGFloat(0.10), CGFloat(0.01))

        let allBoxDims = [box1Dim, box2Dim, box3Dim, box4Dim, box5Dim, box6Dim, box7Dim, box8Dim,box9Dim,box10Dim,box11Dim,box12Dim,box13Dim,box14Dim,box15Dim ]

        let offset: Int = 50
        var boxCounter: Int = 0
        for xIndex: Int in 0...2 {
            for yIndex: Int in 0...4 {
                // create a geometry
                let boxDim = allBoxDims[boxCounter]
                geo = SCNBox(width: boxDim.width, height: boxDim.height, length: boxDim.length, chamferRadius: boxDim.chamferRadius)


                let material = SCNMaterial()
                material.diffuse.contents = image
                geo.firstMaterial = material
                boxCounter = boxCounter + 1


                boxnode = SCNNode(geometry: geo)
                boxnode.position.x = Float(xIndex - offset)
                boxnode.position.y = Float(yIndex - offset)
                self.rootNode.addChildNode(boxnode)

            }
        }
    }

    func deviceDidMove(motion: CMDeviceMotion?, error: NSError?) {
        if let motion = motion {
            boxnode.orientation = motion.gaze(atOrientation: UIApplication.shared.statusBarOrientation)
            if error != nil {
                print("DEVICEDIDMOVE: \(error?.localizedDescription)")
            }
        }
    }
}
John
  • 799
  • 6
  • 22
  • How does what you see when you run that code differ from what you expect to see? – Hal Mueller Jan 01 '17 at 07:19
  • Hi @HalMueller, I expect to load the images from firebase. – John Jan 02 '17 at 00:12
  • I'm missing something, I hope. Replace the "let img" call in the loop with the load from Firebase (which you said you know how to do). – Hal Mueller Jan 02 '17 at 00:39
  • Could you show me an example please? – John Jan 02 '17 at 19:06
  • Add your Firebase code to your question and tell us where you're getting stuck. – Hal Mueller Jan 02 '17 at 19:09
  • I just updated my question. – John Jan 02 '17 at 21:40
  • I don't know what the problem is. I just can't see the pictures. Please help. – John Jan 03 '17 at 05:54
  • With respect, it looks to me like you're trying to solve the problem without understanding the flow of your solution. So here's a hint on how to start. Begin with a simple case: retrieve an image from Firebase and display it in an image view. Then you can retrieve that same image, but put it on your Box node. Finally, you can retrieve a bunch of images from Firebase, and display them on multiple Box nodes. It doesn't look to me like you've managed the first step yet. – Hal Mueller Jan 03 '17 at 07:13

0 Answers0