当前位置:首页 > uni-app

uniapp 编辑图片

2026-03-05 03:27:48uni-app

图片裁剪与压缩

使用 uni.chooseImage 选择图片后,可通过 uni.compressImage 压缩图片。示例代码:

uni.chooseImage({
  count: 1,
  success: (res) => {
    uni.compressImage({
      src: res.tempFilePaths[0],
      quality: 80, // 压缩质量
      success: (compressedRes) => {
        console.log(compressedRes.tempFilePath);
      }
    });
  }
});

图片滤镜与特效

借助第三方插件如 uView UI 或手动实现滤镜效果。通过 Canvas 或 CSS 滤镜属性调整图片样式:

uniapp 编辑图片

// Canvas 实现灰度滤镜
const ctx = uni.createCanvasContext('myCanvas');
ctx.drawImage(tempFilePath, 0, 0, 200, 200);
ctx.setFillStyle('rgba(0, 0, 0, 0.5)');
ctx.fillRect(0, 0, 200, 200);
ctx.draw();

图片旋转与缩放

通过 uni.getImageInfo 获取图片信息后,使用 CSS 或 Canvas 调整:

uniapp 编辑图片

uni.getImageInfo({
  src: tempFilePath,
  success: (info) => {
    const { width, height } = info;
    // 缩放逻辑
    const scaleRatio = Math.min(200 / width, 200 / height);
    this.setData({ imgStyle: `transform: scale(${scaleRatio})` });
  }
});

图片标注与涂鸦

利用 Canvas 实现涂鸦功能,监听触摸事件绘制路径:

let points = [];
Page({
  touchStart(e) {
    points.push({ x: e.touches[0].x, y: e.touches[0].y });
  },
  touchMove(e) {
    const ctx = uni.createCanvasContext('canvas');
    ctx.setStrokeStyle('red');
    ctx.setLineWidth(2);
    ctx.moveTo(points[points.length - 1].x, points[points.length - 1].y);
    ctx.lineTo(e.touches[0].x, e.touches[0].y);
    ctx.stroke();
    ctx.draw(true);
    points.push({ x: e.touches[0].x, y: e.touches[0].y });
  }
});

图片保存与分享

编辑完成后,调用 uni.saveImageToPhotosAlbum 保存至相册,或 uni.share 分享:

uni.saveImageToPhotosAlbum({
  filePath: tempFilePath,
  success: () => {
    uni.showToast({ title: '保存成功' });
  }
});

注意事项

  • 图片处理涉及权限时,需在 manifest.json 中配置 requiredPrivateInfos(如 iOS 的相册权限)。
  • 大图处理建议分片操作,避免内存溢出。
  • 跨平台差异需测试,如 Canvas 在 H5 和小程序中的 API 差异。

标签: 编辑图片
分享给朋友:

相关文章

css3手工制作图片

css3手工制作图片

使用CSS3手工制作图片 CSS3可以通过各种属性如box-shadow、border-radius、gradient等来手工绘制简单的图形或图片效果。以下是几种常见的方法: 绘制圆形 通过bor…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-…

react如何引用图片

react如何引用图片

在React中引用图片的方法 使用import直接引入图片 在React组件中,可以通过import语句将图片作为模块引入。这种方式适合在组件内部使用图片,Webpack会处理图片的路径和打包。 i…

vue实现展示图片

vue实现展示图片

在 Vue 中展示图片的方法 静态图片展示 静态图片通常存放在项目的 public 或 assets 目录下。使用 img 标签直接引用。 <template> <img sr…

js实现图片的拖动

js实现图片的拖动

实现图片拖动的步骤 HTML 结构 在 HTML 中创建一个可拖动的图片元素,确保设置 draggable 属性为 true。 <img id="draggable-img" src="ima…

vue图片实现功能

vue图片实现功能

图片上传功能实现 使用 <input type="file"> 结合 Vue 的 v-on:change 事件监听文件选择 <template> <input t…