Nhảy tới nội dung

my.compressImage

compressImage

my.compressImage là API dùng để nén nhiều ảnh (image) cùng một lúc, khiến chúng có dung lượng nhỏ hơn nhưng vẫn giữ nguyên kích thước ban đầu.

Quét mã để trải nghiệm

Xem code mẫu trên Tini Studio

Demo

Trải nghiệm thử với trình giả lập bên dưới

API Params

Thuộc tínhKiểu dữ liệuBắt buộcMô tả
filePathsString ArrayĐường dẫn tới các tập tin ảnh cần nén
compressLevelNumberMức độ nén ảnh, Nhận giá trị từ 0 tới 3; mặc định là 3. Bảng chi tiết ở dưới.
successFunctionCallback function khi nén ảnh thành công
failFunctionCallback function khi nén ảnh bất thành
completeFunctionCallback function khi gọi API hoàn tất bất kể nén ảnh thành công hay thất bại.

Callback function payload

Thuộc tínhKiểu dữ liệuMô tả
filePathsString ArrayChứa các đường đẫn tạm thời của các ảnh đã được nén

Compress level

Mức độMô tả
0Low quality
1Medium quality
2High quality
3Không compress

Sample Code

<view>
<block-header title="Usage" description="Compress image" />
<view class="block-content">
<image class="image-cover" src="{{tempFilePath}}" />
<button class="button-full" onTap="onChooseImage">Choose Image</button>
</view>
<view class="block-content">
<button class="button-full" disabled="{{tempFilePath === undefined}}" onTap="onCompressImage">Compress
Image</button>
</view>
</view>
Page({
data: {
tempFilePath: undefined,
compressedFilePath: undefined
},
onChooseImage() {
my.chooseImage({
count: 1,
success: (res) => {
console.log(res);
this.setData({
tempFilePath: res.filePaths[0]
});
},
fail: (e) => {
console.log(e);
}
});
},
onCompressImage() {
my.compressImage({
filePaths: [this.data.tempFilePath],
compressLevel: 0,
success: (res) => {
console.log(res);
my.alert({
title: 'Compressed',
content: `File path ${res.filePaths}`
});
this.setData({
compressedFilePath: res.filePaths[0]
});
},
fail: (e) => {
console.log(e);
}
});
}
});