如何使图像可用Ionic 2进行缩放

时间:2016-04-13 06:22:17

标签: ionic-framework zoom ionic2

我有一个包含单张图片的页面,如何才能将此图像缩放?

My Page

3 个答案:

答案 0 :(得分:6)

您可以将图像查看器用于Ionic 2 +

  • 安装
  

npm install --save ionic-img-viewer

  • 添加app.module.ts
import { IonicImageViewerModule } from 'ionic-img-viewer';

@NgModule({
  declarations: [
    MyApp, 
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    IonicImageViewerModule
  ],
  • 程序化用法:
  

< img src =" IMAGE_URL" #myImage(click)=" presentImage(myImage)" />

import { ImageViewerController } from 'ionic-img-viewer';

export class MyPage {
  _imageViewerCtrl: ImageViewerController;

  constructor(imageViewerCtrl: ImageViewerController) {
    this._imageViewerCtrl = imageViewerCtrl;
  }

  presentImage(myImage) {
    const imageViewer = this._imageViewerCtrl.create(myImage);
    imageViewer.present(); 
  }
}
     

DEMO:Demo ionic-img-viewer

答案 1 :(得分:1)

我在Ionic论坛上找到了这个解决方法:

HTML:

<ion-scroll (pinch)="pinchEvent($event)" scrollX="true" scrollY="true" zoom="true" style="width:100%;height:100%;text-align:center;">
    <div [ngStyle]="{'background':'url('+src+') no-repeat','width' : width + 'px', 'height' : height + 'px', 'transform' : 'rotate('+rotacion+'deg)', 'background-size' : 'contain', 'background-position' : 'center'}" style="margin-top:50px;margin-bottom:50px;"></div>
</ion-scroll>

.ts方法:

pinchEvent(e) {
    this.width = this.pinchW * e.scale;
    this.height = this.pinchH * e.scale;

    if(this.timeout == null){
        this.timeout = setTimeout(() => {
            this.timeout = null;
            this.updateWidthHeightPinch();
        }, 1000);
    } else {
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
            this.timeout = null;
            this.updateWidthHeightPinch();
        }, 1000);
    }
}

updateWidthHeightPinch() {
    this.pinchW = this.width;
    this.pinchH = this.height;
}

答案 2 :(得分:1)

我编码&#34;捏&#34;事件我自己 - 这很简单(我用它来缩放建筑物地图):

div class="floor-map"
     style="position: relative; left: 0px; top: 0px; height: 0px; overflow: hidden; cursor: move;"
     (touchstart)="mapMoveStart($event)"
     (touchend)="mapMoveStop($event)"
     (touchmove)="mapMove($event)"
     tappable
     #floorMapRef
> 
    <img [style.width]="width + 'px'"
         [style.height]="height + 'px'" ...>
</div>

在mapMove中我有这样的东西:

pinchLenght = -1  // -1 means no pinch
...

mapMoveStart(event) {
    this.pinchLenght = -1; // "reset" pinch
    ...
}

mapMove(event) 
{
    if(event.touches.length==2) { // two fingers detection 
        let length = Math.abs(event.touches[0].clientX-event.touches[1].clientX)
              + Math.abs(event.touches[0].clientY-event.touches[1].clientY);

          if(this.pinchLenght < 0) { // case: first pinch "touch"
            this.pinchLenght = length
          } else {
            let delta = length - this.pinchLenght;
            this.pinchLenght = length;

            if(delta>0) {
              this.zoomMap(1.05);
            }
            if(delta<0) {
              this.zoomMap(0.95);
            }
         }
    }
    ...
}
...

在方法zoomMap(zoomFactor: number)中,我正确地更改了this.heightthis.width(这是{&#39;绑定到<img>代码。)