previewImage.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // 解决uni.previewImage图片模糊问题
  2. // https://blog.csdn.net/iVRJay/article/details/125942024
  3. function v(a, b) {
  4. return +((1000 * a - 1000 * b) / 1000).toFixed(1)
  5. }
  6. module.exports = {
  7. created() {
  8. if (this._setTransform) {
  9. this._setTransform = (x, y, scale, source = '', r, o) => {
  10. if (!(x !== null && x.toString() !== 'NaN' && typeof x === 'number')) {
  11. x = this._translateX || 0
  12. }
  13. if (!(y !== null && y.toString() !== 'NaN' && typeof y === 'number')) {
  14. y = this._translateY || 0
  15. }
  16. x = Number(x.toFixed(1))
  17. y = Number(y.toFixed(1))
  18. scale = Number(scale.toFixed(1))
  19. if (!(this._translateX === x && this._translateY === y)) {
  20. if (!r) {
  21. this.$trigger('change', {}, {
  22. x: v(x, this._scaleOffset.x),
  23. y: v(y, this._scaleOffset.y),
  24. source: source
  25. })
  26. }
  27. }
  28. if (!this.scale) {
  29. scale = this._scale
  30. }
  31. scale = this._adjustScale(scale)
  32. scale = +scale.toFixed(3)
  33. if (o && scale !== this._scale) {
  34. this.$trigger('scale', {}, {
  35. x: x,
  36. y: y,
  37. scale: scale
  38. })
  39. }
  40. var transform = 'translateX(' + x + 'px) translateY(' + y + 'px) scale(' + scale + ')'
  41. this.$el.style.transform = transform
  42. this.$el.style.webkitTransform = transform
  43. this._translateX = x
  44. this._translateY = y
  45. this._scale = scale
  46. }
  47. }
  48. },
  49. destroyed() {
  50. //解决预览模式关闭后,和重复开关预览模式this._setTransform函数无限次执行导致手机卡顿的问题
  51. if (this._FA) {
  52. this._FA.cancel()
  53. }
  54. if (this._SFA) {
  55. this._SFA.cancel()
  56. }
  57. },
  58. methods: {
  59. }
  60. }