@baile555 写道:
IOS:
step1
在RootViewController.mm脚本中引入step2
在 RootViewController.mm 在loadView方法中定义一个静态变量供后文使用
static UIImagePickerController *selectImage = nullptr;
并且在viewDidLoad()方法中设置代理
_picker = [[UIImagePickerController alloc]init];
//遵守代理
_picker.delegate = self;
selectImage = _picker;step3
在 RootViewController.mm中定义一个静态方法,供js调用
+(void) openPhoto{
[[RootViewController alloc] init];
[[RootViewController alloc] selectPhoto]; //打开相册
}step4
在 RootViewController.mm中定义一个打开相册方法
-(void)selectPhoto{
selectImage.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
//是否允许编辑
selectImage.allowsEditing = NO;
UIViewController *topRootViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
// 在这里加一个这个样式的循环
while (topRootViewController.presentedViewController) {
// 这里固定写法
topRootViewController = topRootViewController.presentedViewController;
}
// 然后再进行present操作
[topRootViewController presentViewController:selectImage animated:YES completion:nil];
}step5
在 RootViewController.mm中设置选择图片完成后的回调方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"info--->成功:%@", info);
char temp[255] = {0};
NSString * code = [[NSString alloc] initWithFormat:@"%@", info[UIImagePickerControllerImageURL]];
const char* tcode = [code UTF8String];
sprintf(temp,"cc.vv.anysdk.setPhotoPath('%s')",tcode);
se::ScriptEngine::getInstance()->evalString(temp);
}step6
在 RootViewController.mm中定义上传图片到服务器的函数
+(void) uploadFileAgain :(NSString *) paramString { //paramString 是一个JSO格式
// NSMutableDictionary *params = [paramString JSONValue];
NSMutableDictionary *params = [NSJSONSerialization JSONObjectWithData:[paramString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:0];
NSString *urlString=[params objectForKey:@"sendUrl"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *boundary = @"wfWiEWrgEFA9A78512weF7106A";
request.allHTTPHeaderFields = @{
@"Content-Type":[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]
};
NSMutableData *postData = [[NSMutableData alloc]init];//请求体数据
NSArray *arrayQuery = @[];//空数组
arrayQuery = @[@"clubId",@"token"];
for (NSString *key in params) {
//循环参数按照部分1、2、3那样循环构建每部分数据
NSString *pair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n",boundary,key];
[postData appendData:[pair dataUsingEncoding:NSUTF8StringEncoding]];id value = [params objectForKey:key]; if ([value isKindOfClass:[NSString class]]) { [postData appendData:[value dataUsingEncoding:NSUTF8StringEncoding]]; }else if ([value isKindOfClass:[NSData class]]){ [postData appendData:value]; } [postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; } NSString *imageDirectory = [params objectForKey:@"path"]; NSData *Imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageDirectory]]; UIImage *image = [UIImage imageWithData:Imagedata]; // NSLog(@"开始上传2"); //添加image并转成二进制 NSData* data = UIImagePNGRepresentation(image); //文件部分
// NSString *filename = [filePath lastPathComponent];
NSString *filename = @"test.png";
NSString *fileKey = @"file";
NSString *contentType = @"application/octet-stream";
NSString *filePair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\" ; ContentType=%@\r\n\r\n",boundary,fileKey,filename,contentType];
[postData appendData:[filePair dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:data]; //加入文件的数据
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//设置结尾
[postData appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
request.HTTPBody = postData;
//设置请求头总数据长度
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
帖子: 1
参与者: 1