控制器注入$scope,$ionicActionSheet,$timeout,$ionicLoading
/*************************************************************************************************/
$scope.choosePicMenu = function() {
var type = 'gallery';
var hideSheet = $ionicActionSheet.show({
buttons: [
{ text: '拍照' },
{ text: '从相册选择' }
],
titleText: '选择照片',
cancelText: '取消',
cancel: function() {
},
buttonClicked: function(index) {
if(index == 0){
//type = 'camera';
var srcType = Camera.PictureSourceType.CAMERA;
}else if(index == 1){
//type = 'gallery';
var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
}
var options = setOptions(srcType);
//var func = createNewFileEntry;
options.targetHeight = 400;//图片压缩后的高
options.targetWidth = 400;
navigator.camera.getPicture(function cameraSuccess(imageUri) {
console.log(imageUri);
$scope.temp_image = imageUri;//取到的本地图片地址,可在页面预览
//$scope.head_logo = imageUri;
$scope.uoload_img();//访问API,保存图片
//弹出缓冲提示
$scope.showLoadingToast();
//这里使用定时器是为了缓存一下加载过程,防止加载过快
$timeout(function () {
//停止缓冲提示
$scope.hideLoadingToast();
}, 1000);
}, function cameraError(error) {
console.debug("Unable to obtain picture: " + error, "app");
//显示等待
//弹出缓冲提示
$scope.showLoadingToast();
//这里使用定时器是为了缓存一下加载过程,防止加载过快
$timeout(function () {
//停止缓冲提示
$scope.hideLoadingToast();
}, 2500);
}, options);
}
});
$timeout(function() {
hideSheet();
}, 3000);
};
$scope.showLoadingToast = function () {
// Setup the loader
$ionicLoading.show({
template: '请稍后...',
content: 'Loading',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0
});
}
$scope.hideLoadingToast = function () {
$ionicLoading.hide();
}
//请求服务器保存图片地址
$scope.uoload_img = function(){
$ionicLoading.show({///小蓝圈缓冲
template:'<ion-spinner icon="ios-small" class="spinner-assertive"></ion-spinner>',
duration:3000
});
//新建文件上传选项,并设置文件key,name,type
console.log($scope.temp_image);
var options = new FileUploadOptions();
options.fileKey = "upfile";
options.fileName = $scope.temp_image.substr($scope.temp_image.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
//用params保存其他参数,例如昵称,年龄之类
var params = {};
params['userid'] = login_obj.userid;
params['sellerid'] = sellerid;
params['checkstr'] = login_obj.checkstr;
params['fileName'] = options.fileName;
//把params添加到options的params中
options.params = params;
var ft = new FileTransfer();
var upload_url = ****;//接口地址
//上传文件
ft.upload(
$scope.temp_image,
encodeURI(upload_url),
uploadSuccess,
uploadError,
options);
//upload成功的话
function uploadSuccess(r) {
console.log(r);
var resp = JSON.parse(r.response);
//$ionicLoading.hide();
}
}
//upload失败的话
function uploadError(error) {
$ionicLoading.show({
template:"头像上传失败",
duration:2000
});
}
}
/*************************************************************************************************/
//单独的方法,供camera插件调用(配置)
function setOptions(srcType) {
var options = {
// Some common settings are 20, 50, and 100
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
// In this app, dynamically set the picture source, Camera or photo gallery
sourceType: srcType,
encodingType: Camera.EncodingType.JPEG,
mediaType: Camera.MediaType.PICTURE,
allowEdit: true,
correctOrientation: true //Corrects Android orientation quirks
}
return options;
}