Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

ios - Parse array images saving and fetching

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 have an NSMutableArray named imageNameList2 that holds all of the smaller images taken from the larger image. For this example I showed an example with the images being called from the image assets list to make it easier to answer this question.

Here is the imageNameList (NSMutableArray that holds all the smaller images)

var imageNameList: [String] {
    var imageNameList2:[String] = [] //[NSMutableArray]()
    for i in 0...149 {
        let imageName = String(format: "pic_%03d", Int(i))
        imageNameList2.append(imageName)
    }
    return imageNameList2
}

What I'd like to do is have a continue button that will save all these images in order as piffles or any other format to parse that works best and have another button called retrieve that will retrieve all these photos from parse. I basically have a parse server that utilizes parse frameworks to help speed up the backend process. Can you please show me how I would save and retrieve this NSMutableArray if there are different numbers of stored images each time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...