json - 如何将图像添加到服务器上现有的图像数组中? Xcode

标签 json xcode swift file-upload client-server

目前有一个类,以JSON格式向服务器发送1到11个参数(一个文本参数,以及0到10个NSNumber格式参数) 。类的设计使得当格式更改或删除时,服务器上已有的所有数据都将被保存并成为新的,即完全重写。最大数据量仅为 2.7 KB,这并不是很严重。 Quest 的内容是“组织对图像的引用数组”,您只需在当前类中添加图像数组即可。但是有几个问题: 1. 如果服务器已经有一张图片了,我们简单的再添加一张,结果发现已经重写了两张图片了,如果是10张呢?这对用户流量来说是一个严重的打击,而且它也没有纠正服务器与应用程序设计的通信。 2 重写超过5张图片时,出现服务器不可用的错误。我想将特定图像发送到服务器上现有的图像数组,并且不会每次都覆盖它们。只需分配给每个图像ID,就可以从服务器应用程序中删除图像。我尝试了很多不同的选择,但未能达到预期的结果。这是当前类代码

 public class AnswersDataServerEntity: DataSetEntity {
    static let DataSetName = "DataSetName"

    var id: String? = "0"
    var streetId: UInt = 0
    var streetName: String? = ""
    var answers: [AnswerServerEntity]? = []
    var documents: [DocumentServerEntity]? = []

    private func save()
    {
        let deleteCommand = DataSetCommand(dataSetAction: DeleteDataSetAction<AnswersDataServerEntity>(dataSetName: AnswersDataServerEntity.DataSetName, data: self)

        deleteCommand.executeWithSuccess(
            {  (commond) -> Void in
                self.add()
            },
            errorHandler: { (error) -> Void in
        })
    }

    private func add()
    {
        let command = DataSetCommand(dataSetAction: AddDataSetAction<AnswersDataServerEntity>(dataSetName: AnswersDataServerEntity.DataSetName, data: self)

        command.executeWithSuccess(
            { (command) -> Void in
                if let _command = command as? DataSetCommand<AnswersDataServerEntity> {
                    NSLog("\(_command.dataSetAction)")
                }
            },
            errorHandler: { (error) -> Void in
        })
    }

    static func saveSelected()
    {
        self.selected().save()
    }

    static private func selected() -> AnswersDataServerEntity
    {
        var result = AnswersDataServerEntity()
        if let selectedCountry = CountryEntity.selected(){
            for answer in AnswerEntity.userAnswers() {
                result.answers?.append(AnswerServerEntity(questionId: answer.questionId))
            }
        }
          for document in DocumentEntity.deserializeDocuments() {
        let documentImage = UIImage(contentsOfFile: document.documentImageURL)
        if let _documentImage = documentImage {
            let imageData = UIImageJPEGRepresentation(_documentImage, 1)
            if let _imageData = imageData {
                let imageString = _imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
                result.documents?.append(DocumentServerEntity(documentId: document.documentId, documentTitle: document.documentTitle, documentImageString: imageString))
            }
        }
    }
        return result
    }
}

public class AnswerServerEntity: DataSetEntity {
    var questionId: Int = 0

    convenience init (questionId: Int){
        self.init()
        self.questionId = questionId
    }
}

public class DocumentServerEntity: DataSetEntity {

    var documentId: Int = 0
    var documentTitle: String = ""
    var documentImageString: String! // this string will to need to decode in base64string that we can to send it as JSON

    convenience init(documentId: Int, documentTitle: String, documentImageString: String) {
        self.init()
        self.documentId = documentId
        self.documentTitle = documentTitle
        self.documentImageString = documentImageString
    }
}

来自服务器的结果,以便更好地了解发生的情况。

  request: {
    "object" : "storage",
    "section" : "api",
    "method" : "addData",
    "data" : [
    {
    "answers" : [
    {
    "questionId" : 201
    },
    {
    "questionId" : 203
    },
    {
    "questionId" : 206
    },
    {
    "questionId" : 210
    }
    ],
    "documents" : [
    {
    "documentImageString" : "\/var\/mobile\/Containers\/Data\/Application\/DD6F8D10-1241-4119-9639-AB983B27CFA6\/Documents\/04DDA484-F257-43BC-A459-3BC7C6050D6F",
    "documentTitle" : "xcode",
    "documentId" : 0
}
],
"streetName" : "Example",
"id" : "0",
"streetId" : 14
}
],
"token" : "**********************$$$$*$*$*$",
"dataSet" : "DataSetName"
}

最佳答案

我使用公司的api将图像发送到服务器

  static func save(document: DocumentEntity) {
        let image = UIImage(contentsOfFile: document.documentImageURL)
        if let _image = image {
            let uploadCommand = UploadCommand(image: _image)
            uploadCommand.executeWithSuccess({ (success) -> Void in
                if let _success = success as? UploadCommand {
                    if let _outFileName = _success.outFileName {
                        var documentArray = upload()
                        documentArray.append(DocumentPreServerEntity(documentId: document.documentId, documentTitle: document.documentTitle, documentImageURL: _outFileName))
                        let documentsData = NSKeyedArchiver.archivedDataWithRootObject(documentArray)
                        NSUserDefaults.standardUserDefaults().setObject(documentsData, forKey: DocumentPreServerEntity.documentServerEntity)
                        NSUserDefaults.standardUserDefaults().synchronize()
                        AnswersDataServerEntity.saveSelected()
                    }
                }
                }, errorHandler: { (error) -> Void in
                    print(error.localizedDescription)
            })
        }
    }

关于json - 如何将图像添加到服务器上现有的图像数组中? Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35865884/

相关文章:

c# - 将数组的 JSON 数组反序列化为 c# 类

javascript - 如何从 AJAX 调用中返回一个数组?

json - NSCoding 和 Codable 属性 <=> JSON 格式 <=>(读/写)文件

iphone - NSString 到 UIImage 类型的参数

ios - 红色 cocoa pod 文件

iphone - 如何从 Xcode 通过短信发送音乐文件

swift - 使用 Swift 2.2 扩展字典数组

javascript - 在Node-Red中解析JSON JavaScript

ios - 如何使用 for 循环将选项卡添加到 TabLayout

ios - NSURLSessionDataDelegate 未调用