如何在IOS上使用nativescript-camera和nativescript-background-http上传图像

时间:2018-12-14 15:23:34

标签: ios nativescript angular2-nativescript

我可以使用nativescript-cameranativescript-background-http将android上的文件上传到C#网络api。

我将参数设置如下:

  var params = [{ name: "image", filename: fileUri, mimeType: 'application/octet-stream' }];

...

并发送请求:

let request = {
        url: this.API_URL + "/UploadOctetFile",
        method: "POST",
        headers: {
          "Authorization": accessTokenJson,
          "Content-Type": "application/octet-stream",
          "File-Name": imageName
        },  
        description: "{ 'uploading': " + imageName + " }"
      };

      return this.session.multipartUpload(params, request);

在android中,fileUri以一种相当直接的方式返回:

takePictrue.then((imageAsset: ImageAsset) => {

然后imageAsset.android拥有发送图像所需的文件uri。但是,对于ios,imageAsset.ios是PHAsset。要获取文件URI,我使用以下代码:

        let manager = PHImageManager.defaultManager()
        let options = new PHImageRequestOptions();
        options.resizeMode = PHImageRequestOptionsResizeMode.Exact;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

manager.requestImageForAssetTargetSizeContentModeOptionsResultHandler(imageAsset.ios,{宽度:2048,高度:1536},PHImageContentModeAspectFill,选项,函数(结果,信息){             让srcfilePath = info.objectForKey(“ PHImageFileURLKey”)。toString();                ...         });

在上面:srcfilePath(我作为fileUri传递)是:file:///var/mobile/Media/DCIM/101APPLE/IMG_1902.JPG

图片名称为IMG_1902.JPG

但是,这会上传一个空文件。我猜测我需要更改fileUri,或者我应该使用其他方法来更改requestImageForAssetTargetSizeContentModeOptionsResultHandler

我尝试了建议here的解决方案:

 let fileUri = image.fileUri.replace("file://","");

,并且我尝试更改了哑剧类型"mimeType":"image/jpg"

任何想法都值得赞赏。

1 个答案:

答案 0 :(得分:0)

山姆,这可能会有所帮助。我做了以下事情来处理iOS用户,从他们的画廊中选择一张照片供以后在应用中使用(Android很简单明了,但iOS返回PHAsset):

import { ImageSource } from "image-source";
import { Image } from "tns-core-modules/ui/image";
import { path, knownFolders } from "tns-core-modules/file-system";

//iOS user picks photo...

const iosImg = new ImageSource();
iosImg.fromAsset(myPHAsset).then(imsr => {
  this.counter += 1; //class property to allow unique filenames
  const folder = knownFolders.documents();
  const path2 = path.join(folder.path, `Image${this.counter}.jpg`);
  const saved = imsr.saveToFile(path2, "jpg");
  const img = new Image();
  img.src = path2;
  this.data.changeImage(img); //Ng service to allow use of image in other components
});

链接:{N} docs

{N} imagepicker #197

{N} imageUpload code