iOS 相册权限 nativescript

iOS photo album permissions nativescript

您好,我正在尝试访问 iOS 上的用户相册,以便用户在我的应用程序中 select 个人资料照片。

我正在使用本机脚本,但我不知道如何请求这样做的权限。我的错误是:

 CONSOLE LOG file:///app/shared/user-view-model/user-view-model.js:96:28: fileUri: file:///var/mobile/Media/DCIM/100APPLE/IMG_0002.JPG
    CONSOLE LOG file:///app/shared/user-view-model/user-view-model.js:101:24: NSErrorWrapper: You don’t have permission to save the file “100APPLE” in the folder “DCIM”.

我用过

<key>NSPhotoLibraryUsageDescription</key>
<string> ${PRODUCT_NAME} photo use</string>

在我的 info.plist 中,但运气不好。有人知道我错过了什么吗?

var frame = require("ui/frame");
var platform = require("platform");
var firebase = require("nativescript-plugin-firebase");
var page;
var list;

function pageLoaded(args) {
    page = args.object;
    list = page.getViewById("urls-list");
}
exports.pageLoaded = pageLoaded;

function onSelectMultipleTap(args) {
    var imagepicker = require("nativescript-imagepicker");
    var context = imagepicker.create({
        mode: "multiple"
    });
    startSelection(context);
}
exports.onSelectMultipleTap = onSelectMultipleTap;

function onSelectSingleTap(args) {
    var imagepicker = require("nativescript-imagepicker");
    var context = imagepicker.create({
        mode: "single"
    });
    startSelection(context);
}
exports.onSelectSingleTap = onSelectSingleTap;

function startSelection(context) {
    context
        .authorize()
        .then(function() {
            list.items = [];
            return context.present();
        })
        .then(function(selection) {
            console.log("Selection done:");
            selection.forEach(function(selected) {
                console.log("----------------");
                console.log("uri: " + selected.uri);
                console.log("fileUri: " + selected.fileUri);
                uploadImage(selected.fileUri);
            });
            list.items = selection;
        }).catch(function (e) {
            console.log(e);
        });
}

function uploadImage(imageUrl){
  firebase.uploadFile({
    remoteFullPath: "test/testimage.jpg",
    localFullPath: imageUrl,
     onProgress: function(status) {
      console.log("Uploaded fraction: " + status.fractionCompleted);
      console.log("Percentage complete: " + status.percentageCompleted);
    }
  }).then((success) => {
    console.log("success: " + success);
  }, (error) => {
    console.log("Error: " + error);
  })
}

你需要这个requestAuthorization

PHPhotoLibrary.requestAuthorization(function (result) {
    if (result === PHAuthorizationStatus.PHAuthorizationStatusAuthorized) {
        // OK
    } else {
        // no permissions!
    }
});

您的info.plist

也需要这个
<dict>
    <key>NSPhotoLibraryUsageDescription</key>
    <string></string>
</dict>

我从这个 github post

中找到了解决方案

https://github.com/NativeScript/sample-ImageUpload/issues/2#issuecomment-252795778

解决方案是使用 getImage() 方法,然后对结果使用 saveToFile 方法。

private selectImage(context){
    context.authorize()
      .then(() => {return context.present()})
      .then((selection) => {
        selection.forEach((selected) => {
          selected.getImage()
            .then((imageSource) => {
              var appPath = fs.knownFolders.currentApp().path;
              var profilePath = "/img/profile.jpg";
              var filePath = fs.path.join(appPath, profilePath);
              var saved = imageSource.saveToFile(filePath, enums.ImageFormat.jpeg);
              console.log(`item saved:${saved}`);
              console.log("FilePath: " + filePath);
              console.log("Image source " + JSON.stringify(imageSource));
            },
            (error) => {
              console.log("error");
            })
        })
      })
  }