在我的情况下,出现的是黑色图像,而不是显示裁切后的图像。我也尝试过使用image.onload
。可能是什么原因?
代码:
const canvas = document.createElement('canvas');
canvas.width = pixelCrop.width;
canvas.height = pixelCrop.height;
const ctx = canvas.getContext('2d');
const newImage = new Image();
ctx.drawImage(
newImage,
pixelCrop.x,
pixelCrop.y,
pixelCrop.width,
pixelCrop.height,
0,
0,
pixelCrop.width,
pixelCrop.height
);
//image -- <img src='url'/>
newImage.src = image.src;
return new Promise((resolve, reject) => {
canvas.toBlob((blob) => {
console.log('blob is', blob);
blob.name = fileName;
window.URL.revokeObjectURL(this.fileUrl);
this.fileUrl = window.URL.createObjectURL(blob);
resolve(this.fileUrl);
}, 'image/jpeg');
});