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

时间:2016-03-08 11:16:32

标签: json xcode swift file-upload client-server

目前,有一个类以JSON格式(文本参数和0到10 NSNumber格式参数)向服务器发送1到11个参数。并且类的设计使得当格式更改或删除已经存在于服务器上的所有数据时都保存并且是新的,即完全重写。最大数据量仅为2.7千字节,这并非可怕。 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"
}

1 个答案:

答案 0 :(得分:0)

我使用公司的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)
            })
        }
    }