更新记录

1.0.0(2025-02-10)

发布九宫格切图功能


平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.92 app-vue
钉钉小程序 快手小程序 飞书小程序 京东小程序 鸿蒙元服务
×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× ×

MagicPicNineSquare 九宫格切图

组件名:magic-pic-nine-square

将一张图片等比例切成9张图片,常用在:朋友圈切宫格图、图片处理等。支持旋转,使用简单,全渠道通用。

九宫格切图组件

注意事项 (1)该组件不包括预览功能,组件生成图片后会直接返回图片地址,开发者可以拿图片自行开发预览功能。

(2)使用组件的时候,请锁死左右滑动,不然会出现白屏。【overflow-x: hidden;】

安装方式

本组件符合easycom规范,HBuilderX 2.5.5起,只需将本组件导入项目,在页面template中即可直接使用,无需在页面中import和注册components

基本用法

template 中使用组件

<view>
    <magic-pic-nine-square ref="magicPicNineSquare"></magic-pic-nine-square>
</view>

使用方法

<view>
    <magic-pic-nine-square ref="magicPicNineSquare"></magic-pic-nine-square>
</view>
export default {
    data() {
        return {
      squareImg: null,
      mainPhoto: {
        width: null,
        height: null
      }
    };
    },
    methods: {
        // 从相册中选择图片
    choosePhotoImage() {
        this.$refs.magicPicNineSquare.choosePhotoImage().then((photo) => {
            // photo - 拿到获取的图片实例
            this.squareImg = photo.path;
            // TODO 自己的业务逻辑
            // ...

            // 生成图片逻辑
            this.takeImage();
        })
    },
    // 从微信聊天记录中选择图片 支持在微信小程序中使用
    chooseWeiXinImage() {
        this.$refs.magicPicNineSquare.chooseWeiXinImage().then((photo) => {
            // photo - 拿到获取的图片实例
            this.squareImg = photo.path;
            // TODO 自己的业务逻辑
            // ...

            // 生成图片逻辑
            this.takeImage();
        })
    },
    // 生成图片
    takeImage() {
        // this.squareImg - 图片地址
        // this.rotate - 旋转角度
        this.$refs.magicPicNineSquare.takeImage(this.squareImg, this.rotate).then(({ imgList, image }) => {
            // image 涉及旋转,旋转后新的图片尺寸,用户定义预览功能是多少的宽高
            // 这里给出在屏幕居中展示的情况下,宽高计算方式,其他的请自行编写
            if(image.width >= image.height) {
                this.mainPhoto.width = this.system.windowWidth;
                this.mainPhoto.height = image.height * this.mainPhoto.width / image.width;
            } else {
                this.mainPhoto.height = this.system.windowHeight;
                this.mainPhoto.width = image.width * this.mainPhoto.height / image.height;
            }
            // imgList - 生成的九宫格图片链接
        })
    },
    // 保存图片
    saveImageHandle() {
        this.$refs.magicPicNineSquare.saveImageHandle().then((data) => {
            console.log(data)
        })
    },
    }
};

API

Calendar Props

属性名 类型 默认值 说明
delay Number 500 延时生成图片,图片在旋转后如果立即生成9宫格图片,会造成渲染异常,故增加延时时间,默认0.5秒,可以自行调整

Calendar Events

事件名 出参方式 说明 入参 返回值
getPhoto return 获取上传的图片实例 - param1:{ path: '图片虚拟地址,可以直接放在image标签里面', size: '图片大小' }
getPhotoSize Promise 获取上传的图片尺寸信息 - 出参参考 @tutorial https://uniapp.dcloud.net.cn/api/media/image.html#getimageinfo 的success出参
choosePhotoImage Promise 从相册中获取图片 - param1:{ path: '图片虚拟地址,可以直接放在image标签里面', size: '图片大小' }
chooseWeiXinImage Promise 从微信聊天中获取图片 只支持微信小程序 - param1:{ path: '图片虚拟地址,可以直接放在image标签里面', size: '图片大小' }
takeImage Promise 进行九宫格处理图片 tempFilePath {String} 图片地址
rotate {Number} 旋转角度,只支持四选一[0|90|180|270]
param1:{ imgList: '9张图片list', image: '旋转后的图片尺寸,角度为0为原图片尺寸' }

隐私、权限声明

1. 本插件需要申请的系统权限列表:

保存图片到相册权限

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问