ionic可以使用camera插件上传照片,不过次方法一次只能选择一张照片,首先我们先在ionic下载相机插件
$ ionic cordova plugin add cordova-plugin-camera
$ npm install --save @ionic-native/camera
在app.module.ts中声明相机组件,然后在对应的ts文件中写相应方法。 gerPictureByLibrary(){
const options: CameraOptions = {
quality: 100,//图片质量
destinationType: this.camera.DestinationType.FILE_URI,//相机输出值的格式,DATA_URL:返回base64编码的字符串,太大,易崩溃,FILE_URI:安卓文件URI,NATIVE_URI:ios
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,//设置图片的来源
// allowEdit:true,//是否允许编辑
encodingType: this.camera.EncodingType.JPEG,//选择返回的图像文件的编码
mediaType: this.camera.MediaType.ALLMEDIA,//选择媒体类型,根据sourceType确定
}
this.camera.getPicture(options).then((imageURI) => {
this.images.unshift({
src: imageURI
})
}, (err) => {
// Handle error
});
记得在construer中添加组件,然后在对应的html中写一个click就ok啦。