mirror of
https://gitee.com/java110/PropertyApp.git
synced 2026-02-24 05:46:09 +08:00
优化小程序bug
This commit is contained in:
parent
0ade36caaf
commit
4b5f41835f
100
lib/java110/utils/translate-image-0.9.js
Normal file
100
lib/java110/utils/translate-image-0.9.js
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/**
|
||||||
|
|
||||||
|
* 压缩
|
||||||
|
|
||||||
|
* @param {Object} imgSrc 图片url
|
||||||
|
|
||||||
|
* @param {Object} callback 回调设置返回值
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function translate(imgSrc, callback) {
|
||||||
|
|
||||||
|
var img = new Image();
|
||||||
|
|
||||||
|
img.src = imgSrc;
|
||||||
|
|
||||||
|
img.onload = function () {
|
||||||
|
|
||||||
|
var that = this;
|
||||||
|
|
||||||
|
var h = that.height;
|
||||||
|
|
||||||
|
// 默认按比例压缩
|
||||||
|
|
||||||
|
var w = that.width;
|
||||||
|
|
||||||
|
var canvas = document.createElement('canvas');
|
||||||
|
|
||||||
|
var ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
|
var anw = document.createAttribute("width");
|
||||||
|
|
||||||
|
anw.nodeValue = w;
|
||||||
|
|
||||||
|
var anh = document.createAttribute("height");
|
||||||
|
|
||||||
|
anh.nodeValue = h;
|
||||||
|
|
||||||
|
canvas.setAttributeNode(anw);
|
||||||
|
|
||||||
|
canvas.setAttributeNode(anh);
|
||||||
|
|
||||||
|
ctx.drawImage(that, 0, 0, w, h);
|
||||||
|
|
||||||
|
//压缩比例
|
||||||
|
|
||||||
|
var quality = 0.3;
|
||||||
|
|
||||||
|
var base64 = canvas.toDataURL('image/jpeg', quality);
|
||||||
|
|
||||||
|
canvas = null;
|
||||||
|
|
||||||
|
// var blob = base64ToBlob(base64);
|
||||||
|
|
||||||
|
// console.log(333);
|
||||||
|
// console.log(base64)
|
||||||
|
|
||||||
|
//Blob对象转blob地址
|
||||||
|
|
||||||
|
// var blobUrl = window.URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
callback(base64);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
|
||||||
|
* base转Blob对象
|
||||||
|
|
||||||
|
* @param {Object} base64 base64地址
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function base64ToBlob(base64) {
|
||||||
|
|
||||||
|
var arr = base64.split(','),
|
||||||
|
|
||||||
|
mime = arr[0].match(/:(.*?);/)[1],
|
||||||
|
|
||||||
|
bstr = atob(arr[1]),
|
||||||
|
|
||||||
|
n = bstr.length,
|
||||||
|
|
||||||
|
u8arr = new Uint8Array(n);
|
||||||
|
|
||||||
|
while (n--) {
|
||||||
|
|
||||||
|
u8arr[n] = bstr.charCodeAt(n);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Blob([u8arr], {
|
||||||
|
|
||||||
|
type: mime
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,100 +1,67 @@
|
|||||||
/**
|
/**
|
||||||
|
* 压缩
|
||||||
* 压缩
|
*
|
||||||
|
* @param {Object} imgSrc 图片url
|
||||||
* @param {Object} imgSrc 图片url
|
* @param {Object} callback 回调设置返回值
|
||||||
|
*/
|
||||||
* @param {Object} callback 回调设置返回值
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function translate(imgSrc, callback) {
|
export function translate(imgSrc, callback) {
|
||||||
|
imageToBease64(imgSrc, callback)
|
||||||
var img = new Image();
|
|
||||||
|
|
||||||
img.src = imgSrc;
|
|
||||||
|
|
||||||
img.onload = function () {
|
|
||||||
|
|
||||||
var that = this;
|
|
||||||
|
|
||||||
var h = that.height;
|
|
||||||
|
|
||||||
// 默认按比例压缩
|
|
||||||
|
|
||||||
var w = that.width;
|
|
||||||
|
|
||||||
var canvas = document.createElement('canvas');
|
|
||||||
|
|
||||||
var ctx = canvas.getContext('2d');
|
|
||||||
|
|
||||||
var anw = document.createAttribute("width");
|
|
||||||
|
|
||||||
anw.nodeValue = w;
|
|
||||||
|
|
||||||
var anh = document.createAttribute("height");
|
|
||||||
|
|
||||||
anh.nodeValue = h;
|
|
||||||
|
|
||||||
canvas.setAttributeNode(anw);
|
|
||||||
|
|
||||||
canvas.setAttributeNode(anh);
|
|
||||||
|
|
||||||
ctx.drawImage(that, 0, 0, w, h);
|
|
||||||
|
|
||||||
//压缩比例
|
|
||||||
|
|
||||||
var quality = 0.3;
|
|
||||||
|
|
||||||
var base64 = canvas.toDataURL('image/jpeg', quality);
|
|
||||||
|
|
||||||
canvas = null;
|
|
||||||
|
|
||||||
// var blob = base64ToBlob(base64);
|
|
||||||
|
|
||||||
// console.log(333);
|
|
||||||
// console.log(base64)
|
|
||||||
|
|
||||||
//Blob对象转blob地址
|
|
||||||
|
|
||||||
// var blobUrl = window.URL.createObjectURL(blob);
|
|
||||||
|
|
||||||
callback(base64);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 图片转base64
|
||||||
|
*
|
||||||
|
* @param imageUrl 图片地址
|
||||||
|
* @param callback 回调
|
||||||
|
*/
|
||||||
|
function imageToBease64(imageUrl, callback) {
|
||||||
|
// #ifdef MP-WEIXIN
|
||||||
|
uni.getFileSystemManager().readFile({
|
||||||
|
filePath: imageUrl,
|
||||||
|
encoding: 'base64',
|
||||||
|
success: res => {
|
||||||
|
console.log(res);
|
||||||
|
let base64 = 'data:image/jpeg;base64,' + res.data
|
||||||
|
callback(base64)
|
||||||
|
}, fail: (e) => {
|
||||||
|
console.log("图片转换失败");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// #endif
|
||||||
|
|
||||||
* base转Blob对象
|
// #ifndef MP-WEIXIN
|
||||||
|
uni.request({
|
||||||
* @param {Object} base64 base64地址
|
url: imageUrl,
|
||||||
|
method: 'GET',
|
||||||
*/
|
responseType: 'arraybuffer',
|
||||||
|
success: ress => {
|
||||||
|
let base64 = wx.arrayBufferToBase64(ress.data);
|
||||||
|
base64 = 'data:image/jpeg;base64,' + base64
|
||||||
|
}, fail: (e) => {
|
||||||
|
console.log("图片转换失败");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// #endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* base转Blob对象
|
||||||
|
*
|
||||||
|
* @param {Object} base64 base64地址
|
||||||
|
*/
|
||||||
export function base64ToBlob(base64) {
|
export function base64ToBlob(base64) {
|
||||||
|
|
||||||
var arr = base64.split(','),
|
var arr = base64.split(','),
|
||||||
|
|
||||||
mime = arr[0].match(/:(.*?);/)[1],
|
mime = arr[0].match(/:(.*?);/)[1],
|
||||||
|
|
||||||
bstr = atob(arr[1]),
|
bstr = atob(arr[1]),
|
||||||
|
|
||||||
n = bstr.length,
|
n = bstr.length,
|
||||||
|
|
||||||
u8arr = new Uint8Array(n);
|
u8arr = new Uint8Array(n);
|
||||||
|
|
||||||
while (n--) {
|
while (n--) {
|
||||||
|
|
||||||
u8arr[n] = bstr.charCodeAt(n);
|
u8arr[n] = bstr.charCodeAt(n);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Blob([u8arr], {
|
return new Blob([u8arr], {
|
||||||
|
|
||||||
type: mime
|
type: mime
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -103,11 +103,11 @@
|
|||||||
success: (res) => {
|
success: (res) => {
|
||||||
that.imgList.push(res.tempFilePaths[0]);
|
that.imgList.push(res.tempFilePaths[0]);
|
||||||
var tempFilePaths = res.tempFilePaths[0]
|
var tempFilePaths = res.tempFilePaths[0]
|
||||||
//#ifdef H5
|
|
||||||
TanslateImage.translate(tempFilePaths, (url) => {
|
TanslateImage.translate(tempFilePaths, (url) => {
|
||||||
that.photos.push(url);
|
that.photos.push(url);
|
||||||
})
|
})
|
||||||
//#endif
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@ -178,11 +178,11 @@
|
|||||||
success: (res) => {
|
success: (res) => {
|
||||||
that.$data.imgList.push(res.tempFilePaths[0]);
|
that.$data.imgList.push(res.tempFilePaths[0]);
|
||||||
var tempFilePaths = res.tempFilePaths[0]
|
var tempFilePaths = res.tempFilePaths[0]
|
||||||
//#ifdef H5
|
|
||||||
TanslateImage.translate(tempFilePaths, (url) => {
|
TanslateImage.translate(tempFilePaths, (url) => {
|
||||||
that.photos.push(url);
|
that.photos.push(url);
|
||||||
})
|
})
|
||||||
//#endif
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@ -428,11 +428,11 @@
|
|||||||
success: (res) => {
|
success: (res) => {
|
||||||
that.afterRepairImgList.push(res.tempFilePaths[0]);
|
that.afterRepairImgList.push(res.tempFilePaths[0]);
|
||||||
var tempFilePaths = res.tempFilePaths[0]
|
var tempFilePaths = res.tempFilePaths[0]
|
||||||
//#ifdef H5
|
|
||||||
TanslateImage.translate(tempFilePaths, (url) => {
|
TanslateImage.translate(tempFilePaths, (url) => {
|
||||||
that.afterRepairPhotos.push(url);
|
that.afterRepairPhotos.push(url);
|
||||||
})
|
})
|
||||||
//#endif
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@ -103,11 +103,11 @@
|
|||||||
success: (res) => {
|
success: (res) => {
|
||||||
that.imgList.push(res.tempFilePaths[0]);
|
that.imgList.push(res.tempFilePaths[0]);
|
||||||
var tempFilePaths = res.tempFilePaths[0]
|
var tempFilePaths = res.tempFilePaths[0]
|
||||||
//#ifdef H5
|
|
||||||
TanslateImage.translate(tempFilePaths, (url) => {
|
TanslateImage.translate(tempFilePaths, (url) => {
|
||||||
that.photos.push(url);
|
that.photos.push(url);
|
||||||
})
|
})
|
||||||
//#endif
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user