Parse array images saving and fetching

前端 未结 1 1352
傲寒
傲寒 2020-12-20 09:02

I have a mosaic app that takes multiple size photos and breaks them into smaller photos. Depending on the size of the photo, the amount of smaller photos could vary. Now I h

相关标签:
1条回答
  • 2020-12-20 09:19

    I think you're trying to do something like this. This is just an example. There's a lot of work to be done but hopefully this will get you started. I did not run or test this code.

    The idea is to save your images as PFFiles, and create a 'tile' PFObject for each file. Then save all the 'tile' PFObjects to a 'tiles' key of the image PFObject. Then recall the image when you need it by objectId.

    Good luck.

    let appleTiles = ["apple1, apple2, apple3"]
    let orangeTiles = ["orange1, orange2, orange3, orange4, orange5"]
    
    func usage() {
    
        //dont literally run these synchronously like this
        post(appleTiles)
        post(orangeTiles)
        download()
    }
    
    func post(_ tileNames: [String]) {
    
        let image = PFObject(className: "Image")
    
        let tilesPF = tileNames.map({ name in
            let data = UIImagePNGRepresentation(UIImage(named: name))!
            let file = PFFile(data: data)
    
            let tile = PFObject(className: "Tile")
            tile["tile"] = file
        })
    
        image["tiles"] = tilesPF
    
        image?.saveInBackground(block: { responseObject, error in
    
            //you'll want to save the object ID of the PFObject if you want to retrieve a specific image later
        })
    }
    
    func download() {
    
        let query = PFQuery(className: "image")
    
        //add this if you have a specific image you want to get
        query.whereKey("objectId", equalTo: "someObjectId")
    
        query.findObjectsInBackground({ result, error in
    
            //this is probably close to how you'd unwrap everything but again, I didn't test it so...
            if let objects = result as? [PFObject], let first = objects.first, let image = first["image"] as? PFObject, let tiles = image["tiles"] as? [PFObject] {
    
                tiles.forEach({ tile in
    
                    let file = tile["tile"]
    
                    //now you have an individual PFFile for a tile, do something with it
                })
            }
        })
    }
    
    0 讨论(0)
提交回复
热议问题