中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
目前只看到支持质量压缩,未找到其他方式压缩图片到指定大小,如将PixelMap压缩至30KB。
微信扫码分享
photoSelect() { const photoSelectOptions = new picker.PhotoSelectOptions(); photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; photoSelectOptions.maxSelectNumber = 5 const photoPicker = new picker.PhotoViewPicker(); photoPicker.select(photoSelectOptions) .then(async (photoSelectResult) => { let uri = photoSelectResult.photoUris[0] let file = fs.openSync(uri, fs.OpenMode.READ_ONLY) this.compressPictures(file) }) } compressPictures(file: fs.File, data?: ArrayBuffer, size: number = 30, quality: number = 99): void | ArrayBuffer { if (data) { const currentSize = data.byteLength / 1024 if (currentSize <= size || quality === 1) { return data } } const imagePackerApi = image.createImagePacker(); const imageSourceApi = image.createImageSource(file.fd); let packOpts: image.PackingOption = { format: “image/jpeg”, quality: quality } imagePackerApi.packing(imageSourceApi, packOpts) .then((ret: ArrayBuffer) => { if (data && ret.byteLength === data.byteLength) { return ret } return this.compressPictures(file, ret, size, quality-1) }).catch((err: BusinessError) => { console.log(lbh) return false }) }