使用SwiftyJSON将UIImage作为数据读取

时间:2017-06-09 11:07:33

标签: ios swift3

我有一个来自服务器的响应作为json对象,其中一个键将UIImage存储为Data。我怎么能得到这把钥匙的价值?

我现在拥有的:

let json = JSON(data: response.data!)
if json.count > 0 {
for (index , subJson):(String, JSON) in json {
    if let photoThumbString = subJson["photoThumb"].string {
                   if photoThumbString.characters.count > 0 {
                         let data = photoThumbString.data(using: .utf8)!
                         feedObject.pictureData = data
                   }
    }
}

但是如果我将Data对象转换回UIImage,那么图像将为nil:

if let pictureData = currentFeedObject.pictureData {
        let image: UIImage? = UIImage(data: pictureData)
    }

这是我收到的json(照片拇指值非常长,我只放了一小部分)

[
  {
    "id" : "26sb1f20-4c62-1se7-8478-03261423e36b",
    "photoThumb" : "\/9j\/4AAQSkZJRgABAQAASABIAAD\/4QBYRXhpZgAATU0AK\/KpWyIhsV0K74Y0XG4ZYk5yc1o3zNp8cbW\/BlBQ\/TisyL\/X2\/8Au\/1rS1z\/AFNt9T\/Sol8SNOp\/\/9k=",
    "author" : {
      "firstName" : "First Name",
      "lastName" : "Last Name",
      "admin" : false,
      "userPrincipal" : "xxxx"
    },
    "dateTime" : "2017-06-08T16:11:38.434",
    "hashtags" : [
        "#general"
    ],
    "content" : "ASDASDASD"
  }
]

1 个答案:

答案 0 :(得分:0)

请告诉我你是否100%确定" photoThumb"在JSON中是正确的

我认为这个post对你非常有用