QiuXc
QiuXc
  • 发布:2020-11-14 10:25
  • 更新:2023-09-15 13:47
  • 阅读:2932

uni.chooseImage 相机相册选择图片弹框国际化

分类:uni-app

自己封装的一个实现选择图片弹框国际化的方法,返回的格式与 uni.chooseImage 相同,因此可以把 uni.chooseImage 替换掉,在main.js挂到了Vue原型上:

/** 封装国际化选择图片  
 * @param {Object} self 组件中的 this  
 * @param {Object} params 配置参数  
 */  
Vue.prototype.selectImage = (self, params = {}) => {  
    let count = params.count || 9;                                  // 数量  
    let sizeType = params.sizeType || ['original', 'compressed'];   // 原图和压缩图  
    let sourceType = params.sourceType || ['camera', 'album'];      // 相册和相机  
    let success = params.success || function success(){};           // 成功回调  
    let fail = params.fail || function fail(){};                    // 失败回调  
    let option = {  
        cancel: self.$t('prompt.btn2'),          // i18n取消文字  
        buttons: [{  
                title: self.$t('prompt.imgBtn1') // i18n相机文字    
            },  
            {  
                title: self.$t('prompt.imgBtn2') // i18n相册文字    
            }  
        ]  
    };  
    if(!sourceType.includes('camera')) delete option.buttons[0];  
    if(!sourceType.includes('album')) delete option.buttons[1];  
    plus.nativeUI.actionSheet(option,  
        async (tap) => {  
            if(sourceType.length === 2) {  
                if( tap.index === 1) {  
                    // 拍照  
                    const cmr = plus.camera.getCamera();  
                    cmr.captureImage((path) => {  
                        plus.io.getFileInfo({  
                            filePath: path,  
                            success: res => {  
                                let result = {  
                                    errMsg: "chooseImage:ok",  
                                    tempFilePaths: [path],  
                                    tempFiles: [{  
                                        path: path,  
                                        size: res.size  
                                    }]  
                                };  
                                success(result);  
                            },  
                            fail: err => fail(err)  
                        });  
                        }, (err) => {fail(err)});  
                } else if (tap.index === 2) {  
                    // 从相册选择  
                    plus.gallery.pick(  
                    (res) => {  
                        let filesList = res.files;  
                        let tempFiles = [];  
                        for (let path of filesList) {  
                            plus.io.getFileInfo({  
                                filePath: path,  
                                success: r => {  
                                    tempFiles.push({  
                                        path: path,  
                                        size: r.size  
                                    });  
                                    if(tempFiles.length === filesList.length) {  
                                        success({  
                                            errMsg: "chooseImage:ok",  
                                            tempFilePaths: filesList,  
                                            tempFiles  
                                        })  
                                    }  
                                },  
                                fail: err => fail(err)  
                            });  
                        }  
                    }, (err) => {fail(err)},{  
                        filter: "image",  
                        multiple: true,  
                        maximum: count,  
                        system: false,  
                    });  
                }  
            } else if (sourceType.length === 1) {  
                if(sourceType.includes('camera')) {  
                    // 拍照  
                    const cmr = plus.camera.getCamera();  
                    cmr.captureImage((path) => {  
                        plus.io.getFileInfo({  
                            filePath: path,  
                            success: res => {  
                                let result = {  
                                    errMsg: "chooseImage:ok",  
                                    tempFilePaths: [path],  
                                    tempFiles: [{  
                                        path: path,  
                                        size: res.size  
                                    }]  
                                };  
                                success(result);  
                            },  
                            fail: err => fail(err)  
                        });  
                        }, (err) => {fail(err)});  
                } else {  
                    // 从相册选择  
                    plus.gallery.pick(  
                    (res) => {  
                        let filesList = res.files;  
                        let tempFiles = [];  
                        for (let path of filesList) {  
                            plus.io.getFileInfo({  
                                filePath: path,  
                                success: r => {  
                                    tempFiles.push({  
                                        path: path,  
                                        size: r.size  
                                    });  
                                    if(tempFiles.length === filesList.length) {  
                                        success({  
                                            errMsg: "chooseImage:ok",  
                                            tempFilePaths: filesList,  
                                            tempFiles  
                                        })  
                                    }  
                                },  
                                fail: err => fail(err)  
                            });  
                        }  
                    }, (err) => {fail(err)},{  
                        filter: "image",  
                        multiple: true,  
                        maximum: count,  
                        system: false,  
                    });  
                }  
            }  
        });  
}
2 关注 分享
1***@qq.com s***@163.com

要回复文章请先登录注册

1***@qq.com

1***@qq.com

有没有遇到这种情况 ,比如chooseImage 设定最大选择5张图片,用户每次选取小于5张的图片,一次4张一次4张,这样就可以越过最大5张的设定
2023-09-15 13:47
s***@163.com

s***@163.com

666 已经用上
2022-04-22 14:52
1***@qq.com

1***@qq.com

选中图片界面好像没办法自定义
2021-06-08 14:35