您好我刚刚使用图片选择器视图录制视频并将视频格式化电影转换为mp4并更改NSData以进行发布。但它不起作用我有时会得到415错误500错误。请查看以下代码,了解我的实施方式。任何人都可以帮助我解决这个问题。
如果我需要检查我的服务器端团队,他们使用邮递员进行测试,当我执行下面的代码时工作正常,如果可能出现任何错误请不要工作请指教我谢谢
-(void)camerabuttonHandlerqas{
UIAlertController * picker = [UIAlertController alertControllerWithTitle:@"Pick an image using" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }];
UIAlertAction* takeVideo = [UIAlertAction actionWithTitle:@"TakeVideo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
imagePicker.view.tag = 100;
[self presentViewController:imagePicker animated:YES completion:nil]; }];
[picker addAction:picture];
[picker addAction:cancel];
picker.view.tintColor = [UIColor grayColor];
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - <UIImagePickerControllerDelegate>
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
videoURL = info[UIImagePickerControllerMediaURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* videoPath = [NSString stringWithFormat:@"%@/test_%d.mp4", [paths objectAtIndex:0],arc4random_uniform(1000)];
NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
[self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
{
switch (exportSession.status) {
case AVAssetExportSessionStatusFailed:
NSLog(@"status failed");
break;
case AVAssetExportSessionStatusCompleted:{
NSLog(@"status success %@ ",videoPath);
NSMutableDictionary *data = [NSMutableDictionary new];
videoURL = info[UIImagePickerControllerMediaURL];
[data setObject:[self generatePostDataForData:[NSData dataWithContentsOfFile:videoPath]] forKey:@"SelectedvideoURL"];
imagePickerIsSelected = YES;
[self postData:data];
break;
}
}];
[picker dismissViewControllerAnimated:YES completion:nil]; }
- (NSData *)generatePostDataForData:(NSData *)uploadData
{ // Generate the post header:
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"upload[file]\";
filename=\"somefile\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];
// Get the post header int ASCII format:
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Generate the mutable data variable:
NSMutableData *postData = [[NSMutableData alloc] initWithLength: [postHeaderData length] ];
[postData setData:postHeaderData];
// Add the image:
// Add the closing boundry:
[postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
// Return the post data:
return postData;
}
-(void) postData:(NSData *)data
{
service = [NSString stringWithFormat:@"http://1.1.1.1:8080/testing/postapi/photoUpload/uploadVideo?videoName=video_%d&type=mp4",arc4random_uniform(1000)]
delegate = delegateInstance;
service = [service stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLFragmentAllowedCharacterSet]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:service]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
// set Content-Type in HTTP header
[request setValue:@"mp4/MOV" forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// add image data
if (videoData) {
[body appendData:videoData];
}
// setting the body of the post to the reqeust
[request setHTTPBody:videoData];
// set URL
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
} }]; [dataTask resume]; }
答案 0 :(得分:0)
415错误介质类型错误检查存储NSDATA的服务器数据类型。
答案 1 :(得分:0)
我得到的解决方案是代码绝对正确,如果服务器端接受.mov类型,则无需将媒体类型更改为mp4,请与服务器端讨论,然后必须更改内容类型,并将文件作为一个数据然后将其作为一个数据为我工作
谢谢。