本文爲原創文章,轉載請標明出處css
app.module.ts
終端運行:html
ionic cordova plugin add cordova-plugin-camera npm install --save @ionic-native/camera ionic cordova plugin add cordova-plugin-telerik-imagepicker --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="your usage message" npm install --save @ionic-native/image-picker
app.module.ts
... import {Camera} from "@ionic-native/camera"; import {ImagePicker} from "@ionic-native/image-picker"; ... @NgModule({ ... providers: [ ... Camera, ImagePicker, ... ] ... }) ...
終端運行:git
ionic g page edit
edit.html
github
<ion-header> <ion-navbar> <ion-title>編輯</ion-title> <ion-buttons end> <button ion-button>保存</button> </ion-buttons> </ion-navbar> </ion-header> <ion-content> <div class="header-image" tappable [ngStyle]="{'background-image': 'url('+avatar+')'}" (click)="presentActionSheet()"></div> </ion-content>
edit.scss
apache
page-edit { .header-image { width: 100px; height: 100px; border-radius: 50%; margin-top: 20px; margin-left: auto; margin-right: auto; background-size: cover; } }
edit.ts
npm
import {Component} from '@angular/core'; import {IonicPage, NavController, NavParams, ActionSheetController, AlertController} from 'ionic-angular'; import {ImagePicker, ImagePickerOptions} from "@ionic-native/image-picker"; import {Camera, CameraOptions} from "@ionic-native/camera"; @IonicPage() @Component({ selector: 'page-edit', templateUrl: 'edit.html', }) export class EditPage { avatar: string = ""; constructor(public navCtrl: NavController, public navParams: NavParams, public actionSheetCtrl: ActionSheetController, public alertCtrl: AlertController, public imagePicker: ImagePicker, public camera: Camera) { } presentActionSheet() { let actionSheet = this.actionSheetCtrl.create({ buttons: [{ text: '拍照', role: 'takePhoto', handler: () => { this.takePhoto(); } }, { text: '從相冊選擇', role: 'chooseFromAlbum', handler: () => { this.chooseFromAlbum(); } }, { text: '取消', role: 'cancel', handler: () => { console.log("cancel"); } }] }); actionSheet.present().then(value => { return value; }); } takePhoto() { const options: CameraOptions = { quality: 100, allowEdit: true, targetWidth: 200, targetHeight: 200, saveToPhotoAlbum: true, }; this.camera.getPicture(options).then(image => { console.log('Image URI: ' + image); this.avatar = image.slice(7); }, error => { console.log('Error: ' + error); }); } chooseFromAlbum() { const options: ImagePickerOptions = { maximumImagesCount: 1, width: 200, height: 200 }; this.imagePicker.getPictures(options).then(images => { if (images.length > 1) { this.presentAlert(); } else if (images.length === 1) { console.log('Image URI: ' + images[0]); this.avatar = images[0].slice(7); } }, error => { console.log('Error: ' + error); }); } presentAlert() { let alert = this.alertCtrl.create({title: "上傳失敗", message: "只能選擇一張圖片做爲頭像哦", buttons: ["肯定"]}); alert.present().then(value => { return value; }); } }
Ionic Native - Camera
GitHub - cordova-plugin-camera
Ionic Native - Image Picker
GitHub - ImagePickerapp
iOS:
ionic
Android:
ide
若有不當之處,請予指正,謝謝~this