将原始图像数据转换为base64编码的字符串

时间:2016-03-16 04:55:18

标签: ios swift parse-platform httprequest dropbox-api

我正在使用以下API从Dropbox下载缩略图:

https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail

这是我的功能的一部分来自云代码:

  }).then(function(oauthToken){
    /* Retrieve thumbnail image from dropbox */
    return Parse.Cloud.httpRequest({
      method: 'POST',
      url: 'https://content.dropboxapi.com/2/files/get_thumbnail',
      headers: {
        'Authorization': 'Bearer ' + oauthToken.get("accessToken"),
        'Dropbox-API-Arg': JSON.stringify({"path": dropboxFileId})
      }
    });

  }).then(function(imageResponse){
    if (log) {console.log(imageResponse);}
    var imageBuffer = imageResponse.buffer;
    var image = imageBuffer.toString("base64");
    response.success(image);
  }, function(error){
    response.error(error.text);
  });

以下是我的imageResponse

{ status: 200,
  headers: 
   { server: 'nginx',
     date: 'Wed, 16 Mar 2016 04:30:54 GMT',
     'content-type': 'application/octet-stream',
     'content-length': '906',
     connection: 'close',
     'accept-ranges': 'bytes',
     pragma: 'no-cache',
     'cache-control': 'no-cache',
     'original-content-length': '906',
     'dropbox-api-result': '{"name": "HTS-logo-low-res.png", "path_lower": "/hts-logo-low-res.png", "path_display": "/HTS-logo-low-res.png", "id": "id:hrkr_mV5FYAAAAAAAAAAHw", "client_modified": "2015-04-05T04:16:15Z", "server_modified": "2016-02-13T06:08:02Z", "rev": "2a043d0c726", "size": 10506}',
     'x-server-response-time': '190',
     'x-dropbox-request-id': '04bb8d6b45511f2b2d7a278e230ea572',
     'x-robots-tag': 'noindex, nofollow, noimageindex' },
  buffer: <Buffer ef bf bd ef bf bd ef bf bd ef bf bd 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ef bf bd ef bf bd 00 43 00 06 04 05 06 05 04 06 06 05 06 07 07 06 ... >,
  cookies: undefined,
  text: '����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000C\u0000\u0006\u0004\u0005\u0006\u0005\u0004\u0006\u0006\u0005\u0006\u0007\u0007\u0006\b\n\u0010\n\n\t\t\n\u0014\u000e\u000f\f\u0010\u0017\u0014\u0018\u0018\u0017\u0014\u0016\u0016\u001a\u001d%\u001f\u001a\u001b#\u001c\u0016\u0016 , #&\')*)\u0019\u001f-0-(0%()(��\u0000C\u0001\u0007\u0007\u0007\n\b\n\u0013\n\n\u0013(\u001a\u0016\u001a((((((((((((((((((((((((((((((((((((((((((((((((((��\u0000\u0011\b\u0000\u0010\u0000@\u0003\u0001"\u0000\u0002\u0011\u0001\u0003\u0011\u0001��\u0000\u001f\u0000\u0000\u0001\u0005\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b��\u0000�\u0010\u0000\u0002\u0001\u0003\u0003\u0002\u0004\u0003\u0005\u0005\u0004\u0004\u0000\u0000\u0001}\u0001\u0002\u0003\u0000\u0004\u0011\u0005\u0012!1A\u0006\u0013Qa\u0007"q\u00142���\b#B��\u0015R��$3br�\t\n\u0016\u0017\u0018\u0019\u001a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������\u0000\u001f\u0001\u0000\u0003\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b��\u0000�\u0011\u0000\u0002\u0001\u0002\u0004\u0004\u0003\u0004\u0007\u0005\u0004\u0004\u0000\u0001\u0002w\u0000\u0001\u0002\u0003\u0011\u0004\u0005!1\u0006\u0012AQ\u0007aq\u0013"2�\b\u0014B����\t#3R�\u0015br�\n\u0016$4�%�\u0017\u0018\u0019\u001a&\'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������\u0000\f\u0003\u0001\u0000\u0002\u0011\u0003\u0011\u0000?\u0000�;ŷ��鱘\'[s,�\u0013L�\n���{\f�g޹\u000f��I�\\.�w\u0013��H~̡���\u0002�\u001b���\u000f\u0001���@�%�&�O��\u0004z����6�Z:X�"G;�GB}M%N�3��ͧ]4��̪��9`�������0�֕�+\u000b�t \u0006P�\u0001%�z��\u0003�R=ި�G$.� �U��h]���d�\u001d9����u���*O����q��|�\u0007\u0005\\�N\u000e\u0017\u0003�\u0000C�q��;�K���1\u001e��HR��z��~�N+���uy��d�بS咕�0e��91�\u001fC�\u0014���}z����.u\u0019Y�I\u0012�T.�\u0019~�\u0007ߦ\b��+���ߝ��W���_q��'
}

我在XCode中的代码:

if let imageInBase64 = response as? String {
    print(imageInBase64)
    let decodedImage = NSData(base64EncodedString: imageInBase64, options: NSDataBase64DecodingOptions())
    imageView.image = UIImage(data: decodedImage!)
}

以下是imageInBase64的输出:

77+977+977+977+9ABBKRklGAAEBAAABAAEAAO+/ve+/vQBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSjvv73vv70AQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo77+977+9ABEIABAAQAMBIgACEQEDEQHvv73vv70AHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL77+977+9AO+/vRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDLvv73vv73vv70II0Lvv73vv70VUu+/ve+/vSQzYnLvv70JChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl677+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+9AB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC++/ve+/vQDvv70RAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjLvv70IFELvv73vv73vv73vv70JIzNS77+9FWJy77+9ChYkNO+/vSXvv70XGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eu+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/vQAMAwEAAhEDEQA/AO+/vTvFt++/ve+/vemxmCdbcyzvv70TTO+/vQrvv73vv73vv717DO+/vWfeuX8P77+977+9Se+/vVwu77+9dxPvv73vv71Ifsyh77+977+977+9Au+/vRvvv73vv73vv70PAe+/ve+/ve+/vUDvv70l77+9Ju+/vU/vv73vv70Eeu+/ve+/ve+/ve+/vTbvv71aOljvv70iRzvvv71HQn1NJU7vv70z77+977+9zaddNO+/ve+/vcyq77+977+9OWDvv73vv73vv73vv73vv73vv73vv70w77+91pXvv70rC++/vXQgBlDvv70BJe+/vXrvv73vv70D77+9Uj3eqO+/vUckLu+/vSDvv71V77+977+9aF3vv73vv73vv71k77+9HTnvv73vv73vv73vv71177+977+977+9Kk/vv73vv73vv73vv71x77+977+9fO+/vQcFXO+/vU4OFwPvv70AQ++/vXHvv73vv70777+9S++/ve+/ve+/vTEe77+977+9SFLvv73vv71677+977+9fu+/vU4r77+977+977+9dXnvv73vv71k77+92KhT5ZKV77+9MGXvv73vv705Me+/vR9D77+9FO+/ve+/ve+/vX1677+977+977+977+9LnUZWe+/vUkS77+9VC7vv70Zfu+/vQffpgjvv73vv70r77+977+977+9353vv73vv71X77+977+977+9X3Hvv73vv70=

当我运行时,我没有显示图像。我怀疑在将其转换为base64字符串之前,我没有正确处理Dropbox的响应。 Dropbox表示他们的回复是原始图像数据。

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

想出来&amp;谢谢那些回答。问题在于Parse.com模块Parse.Cloud.httpRequest,当你自助托管parse-server和Parse.com时,它的工作方式略有不同。我只需要在模块中包含encoding: null作为选项,一切正常。代码如下:

  }).then(function(oauthToken){
    /* Retrieve thumbnail image from dropbox */
    return Parse.Cloud.httpRequest({
      method: 'POST',
      url: 'https://content.dropboxapi.com/2/files/get_thumbnail',
      headers: {
        'Authorization': 'Bearer ' + oauthToken.get("accessToken"),
        'Dropbox-API-Arg': JSON.stringify({"path": dropboxFileId})
      },
      encoding: null
    });

  }).then(function(imageResponse){
    if (log) {console.log(imageResponse);}
    var imageBuffer = imageResponse.buffer;
    var image = imageBuffer.toString("base64");
    response.success(image);
  }, function(error){
    response.error(error.text);
  });