ionic拍照指令

官网说明的cordovaCameraSource ,关于调用手机相机功能有简单介绍,但是如果ionic中有多个地方使用,就显得繁琐,所以我封装了一个拍照指令,供大家使用
1. 定义drCamera指令

directives.directive('drCamera',['$cordovaCamera','$ionicActionSheet',function($cordovaCamera,$ionicActionSheet){
return {
    restrict:'AE',
    replace:true,
    scope:{drClick:'&',drSrc:'='},
    template:'<img ng-src="{{drSrc}}" ng-click="getPhoto()"/>',
    link:function(scope,element, attrs){
        // 更新链接的属性
        var lnk = element.find('img');
        for (key in attrs.$attr){
            lnk.attr(key,attrs.$attr[key]);
        }
        // 改变图片
        scope.getPhoto  = function(){
            console.log('进入到改变图片的方法');
            document.addEventListener("deviceready", function () {
                console.log('进入到本地相机的调用方法');
                // 图片配置
                var optionsTakePhoto = {
                    quality: 100,
                    destinationType: Camera.DestinationType.DATA_URL,
                    sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM,
                    targetWidth: 400,
                    targetHeight: 400
                }
                /**
                 * 拍照
                 * @type {{quality: number, destinationType: *, sourceType: *, allowEdit: boolean, encodingType: *, targetWidth: number, targetHeight: number, popoverOptions: *, saveToPhotoAlbum: boolean, correctOrientation: boolean}}
                 */
                var  optionsMakePhoto= {
                    quality: 100,
                    destinationType: Camera.DestinationType.DATA_URL,
                    sourceType: Camera.PictureSourceType.CAMERA,
                    allowEdit: true,
                    encodingType: Camera.EncodingType.JPEG,
                    targetWidth: 400,
                    targetHeight: 400,
                    popoverOptions: CameraPopoverOptions,
                    saveToPhotoAlbum: true,
                    correctOrientation:true
                };
                // 图片获取失败
                var _onPhotoFail = function(message){
                    console.log('Photo Failed: ' + message);
                }
                // 图片获取成功
                var _onPhotoDone = function(imageData){
                    console.log('图片获取成功');
                    scope.drClick({imageData:imageData});
                }
                // 获取图片
                var _takePhoto = function(){
                    navigator.camera.getPicture(_onPhotoDone, _onPhotoFail,optionsMakePhoto);
                }
                // 生成图片
                var _makePhoto = function(){
                    navigator.camera.getPicture(_onPhotoDone, _onPhotoFail, optionsTakePhoto);
                }
                $ionicActionSheet.show({
                    titleText :scope.titleText,
                    cancelText: '取消',
                    buttons   : [{text:'拍照'},{text:'从相册中选取'}],
                    cancel    : function(){},
                    buttonClicked : function(index){
                        switch(index){
                            case  1:_takePhoto();break;
                            case  0:
                            default:_makePhoto();break;
                        }
                        return true;
                    }
                });
            }, false);

        }
    }
}
}])

2. html中的指令使用

<dr-camera dr-src="headerPic" id="headerPic" alt="" class="circlepic"
                style="width:80px;height:80px;"
                dr-click="changePic(imageData)"
                />

3. controller的使用

$scope.changePic = function(imageData){
        var imgageDatas=[];
        imgageDatas.push(imageData);
        SmsService.common08($scope.member.ucsId, imageData).then(function(resp){
            if (resp.success){
                var image = document.getElementById('headerPic');
                image.src = "data:image/jpeg;base64," + imageData;
            }
        });
    }

使用指令就不需要每个调用地方重复调用了,指令中得到imageData,将参数传递给调用的controller,剩下的业务逻辑就是controller的事情了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值