我想在实时服务器上传视频,以便PLZ建议我如何操作,代码和教程会更好,我还必须在服务器上制作任何形式来存储数据吗?
我对存储数据一无所知。
此代码用于上传数据:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
//assign the mediatype to a string
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//check the media type string so we can determine if its a video
if ([mediaType isEqualToString:@"public.movie"]){
NSLog(@"got a movie");
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
webData = [NSData dataWithContentsOfURL:videoURL];
NSLog(@"webdata is: %@ ",webData);
}
[self dismissModalViewControllerAnimated:NO];
// Handle a movie capture
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
- (IBAction)uploadVideo:(UIButton*)sender {
[self post:webData];
NSLog(@"posted data %@",webData);
}
- (void)post:(NSData *)fileData
{
NSLog(@"POSTING");
// Generate the postdata:
NSData *postData = [self generatePostDataForData: fileData];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
// Setup the request:
NSMutableURLRequest *uploadRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:SERVER_PATH] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ];
[uploadRequest setHTTPMethod:@"POST"];
[uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
[uploadRequest setHTTPBody:postData];
// Execute the reqest:
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
if (conn)
{
// Connection succeeded (even if a 404 or other non-200 range was returned).
NSLog(@"sucess");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got Server Response" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else
{
// Connection failed (cannot reach server).
NSLog(@"fail");
}
}
- (NSData *)generatePostDataForData:(NSData *)uploadData
{
// Generate the post header:
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"file\"; filename=\"ios-video.mov\"\r\nContent-Type: video/mov\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 file:
[postData appendData: uploadData];
// Add the closing boundry:
[postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
// Return the post data:
return postData;
}
我已经定义了我的服务器路径,就像这样呢?
#define SERVER_PATH @"http://manektech.net/diliptest/"
当我记录它时,我的webdata输出是这样的:
webdata is: <00000014 66747970 71742020 00000000 71742020 00000008 77696465 0003837f 6d646174 00cc4007 00ec99ee d49b2f4d b44830a9 915555df adeb095c ...............>
答案 0 :(得分:0)
如果您想通过TCP套接字/流来实现,请转到 NSStream API。 - 使用此方法创建一个NSOutputStream然后写入缓冲区。
如果您想通过HTTP进行操作,请转到 NSURLConnection 。在HTTPBodyStream中以POST发送数据。
答案 1 :(得分:0)
此处创建带有video. name文件的NSData
对象,当您从相机获取vedio并在此时停止imagePickerController
委托方法调用时,此处将此网址或视频放在{{ 1}}喜欢吼叫......
NSData
之后你可以发送像bellow方法的数据..只需按照我在代码中使用的流程...
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%@",info);
[self dismissModalViewControllerAnimated:YES];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[info objectForKey:UIImagePickerControllerMediaURL]]];
videoData = [[NSData alloc] initWithContentsOfURL:url];
}
我希望这个答案对你有所帮助..