【问题标题】:How to upload an image with nativescript-camera and nativescript-background-http on IOS如何在IOS上使用nativescript-camera和nativescript-background-http上传图片
【发布时间】:2018-12-14 15:23:34
【问题描述】:

我可以使用nativescript-cameranativescript-background-http 将android 上的文件上传到C# web 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, { width: 2048, height: 1536 }, PHImageContentModeAspectFill, options, function (result, info) { 让 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://","");

我已经尝试更改 mime 类型 "mimeType":"image/jpg"

非常感谢任何想法。

【问题讨论】:

  • 我怀疑 iOS 允许我们访问实际的图像文件,为什么不将图像资产写入临时文件并上传?
  • 谢谢。我想你可能是对的。我会在接下来的几天里试一试,然后告诉你。

标签: ios nativescript angular2-nativescript


【解决方案1】:

山姆,这可能会有所帮助。我执行了以下操作来处理 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多