【问题标题】:canvas not drawing complete image from uri画布没有从uri绘制完整的图像
【发布时间】:2016-05-02 17:57:00
【问题描述】:

我正在使用 ionic 并尝试在画布上绘制选定的图像并将其转换为 base64 字符串。 when i select image of low quality/size it appear perfectly fine but when image of high quality is selected, it only shows the upper part of image like image gets cropped.

$scope.getImages = function() {

    $scope.modal.hide();
    // Image picker will load images according to these settings
    var options = {
      maximumImagesCount: 1, // Max number of selected images, I'm using only one for this example
      width: 800,
      height: 800,
      quality: 80            // Higher is better
    };
    $cordovaImagePicker.getPictures(options).then(function (results) {
      // Loop through acquired images
      for (var i = 0; i < results.length; i++) {
        $scope.selectedImage = results[i];
      }

    }, function(error) {
      console.log('Error: ' + JSON.stringify(error));    // In case of error
    });



}

这就是选择图像的方式。问题是如何在画布上绘制完整的选定图像,或者换句话说,如何在循环中获取图像的宽度和高度,因为结果 [i] 仅获取图像的 uri。

【问题讨论】:

    标签: javascript html image canvas ionic-framework


    【解决方案1】:

    这会有所帮助

    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    
    var img = new Image();
    img.onload = function()
    { 
          ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    }
    img.src = imagePath
    

    【讨论】:

    • 请问这将如何在画布上绘制完整的图像?详细说明您的答案,因为我无法理解:(
    • 第代码将图像的高度、宽度设置为画布,因此图像将适合画布
    • 有没有获取所选图像的高度和宽度?因为我现在得到了完整的图像,但是由于它的高宽比,它有点压力。
    • img.height 和 img.width
    猜你喜欢
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多