Ionic3学习笔记(十二)拍照上传图片以及从相册选择图片上传
2017-11-27 00:00
453 查看
本文为原创文章,转载请标明出处
导入
创建 provider
更多
效果图
2. 导入
GitHub - cordova-plugin-camera
Ionic Native - Image Picker
GitHub - ImagePicker
Android:
如有不当之处,请予指正,谢谢~
目录
安装插件导入
app.module.ts
创建 provider
更多
效果图
1. 安装插件
终端运行: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
2. 导入 app.module.ts
... import {Camera} from "@ionic-native/camera"; import {ImagePicker} from "@ionic-native/image-picker"; ... @NgModule({ ... providers: [ ... Camera, ImagePicker, ... ] ... }) ...
3. 创建 provider
终端运行:ionic g page edit
edit.html
<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
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
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; }); } }
4. 更多
Ionic Native - CameraGitHub - cordova-plugin-camera
Ionic Native - Image Picker
GitHub - ImagePicker
5. 效果图
iOS:Android:
如有不当之处,请予指正,谢谢~
相关文章推荐
- Ionic3学习笔记(十二)拍照上传图片以及从相册选择图片上传
- 使用南尘的ImagePicker实现仿微信的相册图片选择以及拍照上传
- Android实现拍照,以及从相册选择图片裁剪功能同时保存在本地
- Android 拍照 以及从本地选择图片 上传
- HTML5 Plus 拍照或者相册选择图片上传
- Android WebView 选择图片并上传(调用相机拍照/相册/选择文件)
- android 客户端通过选择本地相册和拍照上传本地图片到服务器端
- 4000 MUI 拍照和从系统相册选择图片上传
- MUI 单图片压缩上传(拍照+系统相册): 选择立即上传
- Android开发之头像上传(包含拍照,从相册选择图片,裁剪等)
- MUI 单个图片上传预览(拍照+系统相册):先选择->预览->上传提交
- 个人界面 < 头像 > 图片选择(相册,拍照)--如何调用系统的相册,裁剪并且上传
- 图片拍照或相册选择的实现以及裁剪功能
- 从相册中选择或拍照设置并上传头像图片
- HTML5 Plus 拍照或者相册选择图片上传
- 小程序从本地相册选择图片或使用相机拍照wx.chooseImage上传后预览并可以识别图中二维码
- Android拍照,相册选择图片以及Android6.0权限管理
- Android拍照,相册选择图片以及Android6.0权限管理
- Android 拍照 以及从本地选择图片 上传
- Android中相机拍摄照片,以及相册选择图片压缩上传(压缩后保存进SD中)(可用于修改头像等)