m***@worldflying.cn
m***@worldflying.cn
  • 发布:2017-11-13 14:41
  • 更新:2018-08-23 18:52
  • 阅读:2847

NJS如何在ios拍摄或者录像完成后存储资源

分类:Native.js

我这边利用NJS实现了IOS拍摄限时视频的功能,现在需要监听IOS拍摄完后的“确认使用”按钮,并且获取文件对象,进行上传,请问怎么获取文件对象

2017-11-13 14:41 2 条评论 负责人:无 分享
已邀请:
m***@worldflying.cn

m***@worldflying.cn (作者)

NJS拍摄IOS限时视频代码如下(修改了别人的):
var cameraVideo = plus.ios.newObject("UIImagePickerController");
cameraVideo.plusSetAttribute('delegate', cameraVideo);
cameraVideo.plusSetAttribute('sourceType', 1); //设置为摄像头0为相册
var iosArray = plus.ios.newObject('NSMutableArray');
plus.ios.invoke(iosArray, 'addObject:', 'public.movie');
cameraVideo.plusSetAttribute('mediaTypes', iosArray); //设置为录制视频
cameraVideo.plusSetAttribute('videoMaximumDuration', 10.01); //注意,这里的时间一定要保留2为小数,而且第二位小数一定要大于0,我这里进行了测试,用typeof输出变量的类型,发现typeof(10),typeof(10.0),typeof(10.00)输出的都是空,只有在第二位小数大于0的情况下,typeof输出的类型才是number,这样的值才能传入videoMaximumDuration中,这是个大坑
var UIApplicationClass = plus.ios.importClass("UIApplication");
var UIAppObj = UIApplicationClass.sharedApplication();
var del = plus.ios.invoke(UIAppObj, "delegate");
var appWindowObj = plus.ios.invoke(del, "window");
var appRootController = plus.ios.invoke(appWindowObj,"rootViewController");
plus.ios.invoke(appRootController,"presentViewController:animated:completion:",cameraVideo,"YES",null);

  • typer

    给力,兄弟,成功解决了录制时长的问题

    2018-08-15 15:31

  • jspeng

    @jarodxie@foxmail.com:请问怎么拿到他的视频路径?

    2018-08-17 10:50

  • typer

    回复 jspeng:我的代理都还没有调起来。。。你调起来了吗

    2018-08-23 16:57

  • typer

    cameraVideo.plusSetAttribute('delegate', cameraVideo); 应该是cameraVideo.plusSetAttribute('delegate', delegate);

    2018-08-23 17:41

m***@worldflying.cn

m***@worldflying.cn (作者)

var delegate = plus.ios.implements('UIImagePickerController',{
'imagePickerController:didFinishPickingMediaWithInfo:':function(picker,info){
plus.ios.invoke(appRootController,'dismissViewControllerAnimated:completion:','YES',function(){
var fm = plus.ios.newObject("NSFileManager");
var path = plus.io.PRIVATE_DOC+"/video_xx001.mov";
var resource = info.plusGetAttribute("UIImagePickerControllerMediaURL");
plus.ios.invoke("NSURL","fileURLWithPath:",path);
plus.ios.invoke(fm,"copyItemAtURL:toURL:error:",resource,path,null)
});
}
});

现在只求个大神来解决 info为空的问题,我就可以保住这份饭碗了

  • 8***@qq.com

    按照这个方法我这里直接卡死了,请问你解决了吗

    2018-02-23 15:35

  • typer

    也跟着大佬走到了这里了,赞。接下来剩获取视频路径

    2018-08-23 17:42

  • 首席劝退师

    回复 typer:你获取到没有

    2018-08-24 14:14

  • typer

    @909178457@qq.com:没有获取到,获取不到的。delegate的回调函数只支持1个参数,后面的参数都接收不了

    2018-08-24 14:27

  • 首席劝退师

    picker返回的东西看不懂了

    2018-08-24 14:47

  • 首席劝退师

    回复 typer:JSBObject

    [LOG] : native_54

    [LOG] : UIControlTargetAction

    [LOG] : function (){var c,e,b=null;try{for(c=[],e=1;e<arguments.length;e++)c.push(d.warp(arguments[e]));b=a.bridge.execSync2(_Server,"plusSetAttribute",[this.UUID,arguments[0],c],null,!0),b=d.New(b,!0)}catch(f){throw f}return b}

    [LOG] : function (b){var c=null;try{c=a.bridge.execSync2(_Server,"plusGetAttribute",[this.UUID,b],null,!0),c=a.ios.Tool.New(c,!0)}catch(d){throw d}return c}

    [LOG] : function (){return a.android.importClass(this)}

    [LOG] : function (b){var d,e,f,g,h,c=null;try{d="",e=[],f=0;for(g in b){if("string"!=typeof g)return;if(h=b[g],0==f){if(d=g,"undefined"==typeof h){f++;break}d+=":"}else d+=g+":";e.push(h),f++}if(0==f)return;e=window.plus.ios.__Tool.process(e),c=window.plus.bridge.execSync2(_Server,"
    exec",[this.UUID,d,e],null,!0),c=a.ios.__Tool.New(c,!0)}catch(i){throw i}return c}

    2018-08-24 14:48

  • typer

    @909178457@qq.com:同看不懂,问了一个做ios的,说视频路径在第二个参数info中。

    2018-08-24 14:59

  • typer

    @909178457@qq.com:然后,h5+这个delegate不支持多参数传递。所以,UIImagePickerController这个方法行不通

    2018-08-24 15:00

  • 首席劝退师

    会不会两个监听,能每个接收不同的参数

    2018-08-24 16:02

  • 首席劝退师

    回复 typer:你解决没

    2018-08-27 08:56

m***@worldflying.cn

m***@worldflying.cn (作者)

var NSDictionary = plus.ios.importClass("NSDictionary");
var ns = new NSDictionary();
var cameraVideo = plus.ios.newObject("UIImagePickerController");
cameraVideo.plusSetAttribute('delegate', cameraVideo);
cameraVideo.plusSetAttribute('sourceType', 1); //设置为摄像头0为相册
var iosArray = plus.ios.newObject('NSMutableArray');
plus.ios.invoke(iosArray, 'addObject:', 'public.movie');
cameraVideo.plusSetAttribute('mediaTypes', iosArray); //设置为录制视频
cameraVideo.plusSetAttribute('videoMaximumDuration', 5.01); //设置最大时长为10秒

    plus.ios.implements("UIImagePickerControllerDelegate",{'imagePickerController:didFinishPickingMediaWithInfo:':function(cameraVideo,ns){  
        console.log(1);  
    }});  

    plus.ios.implements("UIImagePickerControllerDelegate",{"imagePickerControllerDidCancel:":function(){  
        console.log("cancel");  
    }})  

    var UIApplicationClass = plus.ios.importClass("UIApplication");  
    var UIAppObj = UIApplicationClass.sharedApplication();  
    var del = plus.ios.invoke(UIAppObj, "delegate");  
    var appWindowObj = plus.ios.invoke(del, "window");  
    var appRootController = plus.ios.invoke(appWindowObj,"rootViewController");   
    var result = plus.ios.invoke(appRootController,"presentViewController:animated:completion:",cameraVideo,"YES",null);  

添加了连个代理,一个是成功选择了,一个是取消拍摄了,现在问题是不管是我取消还是选择了,代理都没有触发console.log,这是为什么,我哪里有错误?

m***@worldflying.cn

m***@worldflying.cn (作者)

吐血了:最新代码如下,求告知,为嘛代理不生效
var NSDictionary = plus.ios.importClass("NSDictionary");
var ns = new NSDictionary();
var cameraVideo = plus.ios.newObject("UIImagePickerController");
cameraVideo.plusSetAttribute('sourceType', 1); //设置为摄像头0为相册
var iosArray = plus.ios.newObject('NSMutableArray');
plus.ios.invoke(iosArray, 'addObject:', 'public.movie');
cameraVideo.plusSetAttribute('mediaTypes', iosArray); //设置为录制视频
cameraVideo.plusSetAttribute('videoMaximumDuration', 5.01); //设置最大时长为10秒
var UIApplicationClass = plus.ios.importClass("UIApplication");
var UIAppObj = UIApplicationClass.sharedApplication();
var del = plus.ios.invoke(UIAppObj, "delegate");
var appWindowObj = plus.ios.invoke(del, "window");
var appRootController = plus.ios.invoke(appWindowObj,"rootViewController");
cameraVideo.plusSetAttribute('delegate', appRootController);
plus.ios.implements("UIImagePickerControllerDelegate",{
"imagePickerController:didFinishPickingMediaWithInfo:":function(cameraVideo,ns){
alert("success")
}
});
plus.ios.implements("UIImagePickerControllerDelegate",{
"imagePickerControllerDidCancel:":function(cameraVideo){
console.log("cancel")
}
})
plus.ios.invoke(appRootController,"presentViewController:animated:completion:",cameraVideo,"YES",function(){
console.log('start');
cameraVideo.plusSetAttribute('delegate',appRootController);//论坛说有可能delegate丢失了,在调起录像界面后,重新赋值一遍
});

m***@worldflying.cn

m***@worldflying.cn (作者)

参考“元玉”小哥哥的代码,进行修改,结果,确认选择后回调卡死,取消按钮点击没反应
var NSDictionary = plus.ios.importClass("NSDictionary");
var ns = new NSDictionary();
var cameraVideo = plus.ios.newObject("UIImagePickerController");
var delegate = plus.ios.implements("UIImagePickerControllerDelegate",{
"imagePickerController:didFinishPickingMediaWithInfo:":function(picker,info){
console.log('info:'+arguments.length);
plus.ios.invoke(arguments[0],'dismissViewControllerAnimated:completion:','YES',null);
}
});

// var delegate = plus.ios.implements("UIImagePickerControllerDelegate",{
// "imagePickerControllerDidCancel:":function(picker){
// console.log('info:'+arguments.length);
// plus.ios.invoke(arguments[0],'dismissViewControllerAnimated:completion:','YES',null);
// }
// });
cameraVideo.plusSetAttribute('delegate',delegate);
cameraVideo.plusSetAttribute('sourceType', 1); //设置为摄像头0为相册
var iosArray = plus.ios.newObject('NSMutableArray');
plus.ios.invoke(iosArray, 'addObject:', 'public.movie');
cameraVideo.plusSetAttribute('mediaTypes', iosArray); //设置为录制视频
cameraVideo.plusSetAttribute('videoMaximumDuration', 5.01); //设置最大时长为10秒
var UIApplicationClass = plus.ios.importClass("UIApplication");
var UIAppObj = UIApplicationClass.sharedApplication();
var del = plus.ios.invoke(UIAppObj, "delegate");
var appWindowObj = plus.ios.invoke(del, "window");
var appRootController = plus.ios.invoke(appWindowObj,"rootViewController");
plus.ios.invoke(appRootController,"presentViewController:animated:completion:",cameraVideo,"YES",function(){
console.log('start');
});

m***@worldflying.cn

m***@worldflying.cn (作者)

来人帮忙暖暖贴呀,心真特么累

m***@worldflying.cn

m***@worldflying.cn (作者)

日常水暖

m***@worldflying.cn

m***@worldflying.cn (作者)

日常自暖,等待大神支援 gank

m***@worldflying.cn

m***@worldflying.cn (作者)

修改代码后,可以成功的实现代理,但是无法获取到参数值

m***@worldflying.cn

m***@worldflying.cn (作者)

implements("UIImagePickerController",{
"imagePickerController:didFinishPickingMediaWithInfo:":function(picker,info){
console.log(info)
})

LOG: 空

啊杵

啊杵

问题解决了么?我这把info打印出来,没有UIImagePickerControllerMediaURL这个属性啊;
只有{"TYPE":"JSBObject","UUID":"native_60","className":"UIControlTargetAction"}

q***@qq.com

q***@qq.com

测试过了,没有成功返回,求解决,回暖贴

移动达人

移动达人

帮你顶一下,官方有人能帮忙看一下吗? 也需要这个限定录制时间的功能。

typer

typer - 90后ITboy

http://ask.dcloud.net.cn/question/12048 在这里看到官方15年就说delegate不支持多参数传递 ,从当时开发到现在。。。。 (≖ᴗ≖)✧

  • 首席劝退师

    这个无法监听啊

    2018-08-24 13:54

  • typer

    @909178457@qq.com:能监听成功。注意这里:cameraVideo.plusSetAttribute('delegate', cameraVideo); 应该是cameraVideo.plusSetAttribute('delegate', delegate);

    2018-08-24 14:13

该问题目前已经被锁定, 无法添加新回复