小编典典

解析数组图像以保存和获取

swift

我有一个镶嵌应用程序,可以拍摄多张尺寸的照片并将它们分成较小的照片。根据照片的大小,较小照片的数量可能会有所不同。现在,我有一个NSMutableArray名称imageNameList2,其中包含从较大图像中获取的所有较小图像。在此示例中,我展示了一个示例,该示例从图像资产列表中调用了图像,以使回答此问题更加容易。

这是imageNameListNSMutableArray包含所有较小的图像)

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
}

我想要做的是有一个继续按钮,该按钮将所有这些图像按顺序保存为piffles或其他任何格式来解析,效果最好;还有一个名为“检索”的按钮,它将从解析中检索所有这些照片。我基本上有一个解析服务器,该服务器利用解析框架来帮助加速后端过程。NSMutableArray如果每次存储的图像数量不同,您能告诉我如何保存和检索吗?


阅读 384

收藏
2020-07-07

共1个答案

小编典典

我认为您正在尝试做这样的事情。这只是一个例子。有很多工作要做,但是希望这可以帮助您入门。我没有运行或测试此代码。

想法是将图像另存为PFFiles,并PFObject为每个文件创建一个“平铺” 。然后将所有“ tile”保存PFObject到图像的“
tiles”键中PFObject。然后在需要时调用图像objectId

祝好运。

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