|
// 上传图片(选择图片或拍照)
fnChooseImage() {
var that = this;
api.actionSheet({
title: '选择图片',
cancelTitle: '取消',
buttons: ['拍照', '相册']
}, function (ret, err) {
if (ret) {
var sourceTypes = ['camera', 'album'];
if (ret.buttonIndex == (sourceTypes.length + 1)) {
console.log(sourceTypes);
return;
}
var sourceType = sourceTypes[ret.buttonIndex - 1];
var permission = ret.buttonIndex == 1 ? 'camera' : 'photos';
var resultList = api.hasPermission({
list: [permission]
});
if (resultList[0].granted) {
that.getPicture(sourceType);
} else {
api.confirm({
msg: '应用需要您的授权才能访问' + (permission == 'camera' ? '相机' : '相册'),
buttons: ['取消', '去设置']
}, function (ret1) {
if (ret1.buttonIndex == 2) {
api.requestPermission({
list: [permission],
}, function (ret2) {
if (ret2.list[0].granted) {
that.getPicture(sourceType);
}
});
}
});
}
}
});
},
//选择图片
getPicture(sourceType) {
var that = this;
api.getPicture({
sourceType: sourceType,
encodingType: 'jpg',
allowEdit: true,
quality: 100,
targetWidth: 200,
targetHeight: 200,
}, function (ret, err) {
if (ret) {
console.log(ret);
}
});
},
用这个试试看,亲测有效。 |
|