代码之家  ›  专栏  ›  技术社区  ›  trees_are_great

如何在IOS上使用nativescript摄像头和nativescript背景http上载图像

  •  0
  • trees_are_great  · 技术社区  · 7 年前

    nativescript-camera 具有 nativescript-background-http .

    我将参数设置如下:

      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);
    

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

    然后 imageAsset.android imageAsset.ios 这是一套。要获取文件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,选项,函数(结果,信息){ 让srcfilePath=info.objectForKey(“PHImageFileURLKey”).toString(); ... });

    图像名称为IMG_1902.JPG

    requestImageForAssetTargetSizeContentModeOptionsResultHandler .

    我已经试过了建议的解决办法 here 要做:

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

    "mimeType":"image/jpg" .

    任何想法都非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   James South    7 年前

    山姆,这可能会有帮助。我做了以下操作来处理一个iOS用户从他们的图库中选择一张照片,以便稍后在应用程序中使用(Android与您的情况一样简单明了,但iOS返回一个相集):

    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

    推荐文章