ビデオをサーバーにアップロードすることはできますか?私はイメージが可能であることを知っている。 誰かがちょうどいい方向に私を向けることができる場合、それは素晴らしいでしょう。iPhoneでビデオをアップロードする
おかげ
ビデオをサーバーにアップロードすることはできますか?私はイメージが可能であることを知っている。 誰かがちょうどいい方向に私を向けることができる場合、それは素晴らしいでしょう。iPhoneでビデオをアップロードする
おかげ
はUIImagePickerControllerを見てください。 3.0からは、ビデオの撮影や既存のビデオの選択を許可することができます。あなたがが映画の10分の最大に制限されているドキュメントによると:
クール!ありがとうalot –
編集2015年8月
この回答は真剣に時代遅れになりました。執筆の時点では、多くのオプションやビデオにはサイズが比較的小さかった。 これを今見ているなら、私はAFNetworking
を使ってこれをもっと簡単にするでしょう。アップロードしたファイルをメモリに保存するのではなくファイルからストリーミングし、新しいAppleのバックグラウンドアップロードタスクもサポートします。ここ
ドキュメント:https://github.com/AFNetworking/AFNetworking#creating-an-upload-task
-
はい、これは可能であり、これは私がそれについていった方法です。
メディアピッカーが終了したときに実行される次の機能を実装します。私はどこか別の場所からもらったこのようなものを持っていたポスト機能について
- (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:
[postData appendData: uploadData];
// Add the closing boundry:
[postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
// Return the post data:
return postData;
}
- (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];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[self post:webData];
[webData release];
}
(私はそれを見つけたところ申し訳ありませんが私は知らない):
- (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:@"http://www.example.com:3000/"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease];
[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];
[alert release];
}
else
{
// Connection failed (cannot reach server).
NSLog(@"fail");
}
}
上記のスニペットは、HTTP POSTリクエストを構築し、提出しますそれ。まともなエラー処理が必要な場合は、それを修正して、非同期アップロードを許可するライブラリ(github上のもの)を使用することを検討してください。
また、上記のサーバーのURLに3000のポートがあることに気づきました。 iOS8は、サードパーティのライブラリとあなたを使用する必要がないので、私はデバッグ目的のために
希望をリクエストパラメータを見ることができるように、開発モードではデフォルトのポート3000上のレール・サーバを起動するには、この
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *urlString=[urlvideo path];
NSLog(@"urlString=%@",urlString);
NSString *str = [NSString stringWithFormat:@"you url of server"];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setFile:urlString forKey:@"key foruploadingFile"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request startSynchronous];
NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseStatusCode %@",[request responseString]);
あなたはuiimagepickerコントローラでキャプチャしたビデオファイルを、このコードとgithubからassihttprequestフレームワークを取得する必要があります – chinni
を支援することができますファイルから直接ストリームビデオを解決するcメモリエラーのrucial OUTファイルからそれらをロード中に大きな動画をアップロードしよう:
// If video was returned by UIImagePicker ...
NSURL *videoUrl = [_videoDictionary objectForKey:UIImagePickerControllerMediaURL];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:VIDEO_UPLOAD_LINK]];
[request addValue:@"video" forHTTPHeaderField: @"Content-Type"];
[request setHTTPMethod:@"POST"];
NSInputStream *inputStream = [[NSInputStream alloc] initWithFileAtPath:[videoUrl path]];
[request setHTTPBodyStream:inputStream];
self.uploadConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
iOS7も素晴らしいNSURLSeession/NSURLSessionUploadTaskコンボあなたがファイルから直接ストリーミングしましょう、しかしだけでなくソリューションを提供していますiOSプロセスにタスクを委任することもできます。これにより、アプリケーションが終了してもアップロードが完了します。 もう少しコーディングする必要があります。私はここにすべて書き込む時間がありません(あなたはGoogleにすることができます)。ここ
は最も重要な部分である:Confugure背景サポートにオーディオセッション:
- (NSURLSession *)urlSession {
if (!_urlSession) {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *bundleId = infoDict[@"CFBundleIdentifier"];
NSString *label = [NSString stringWithFormat:@"ATLoggerUploadManager_%@", bundleId];
NSURLSessionConfiguration *conf = (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) ? [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:label] : [NSURLSessionConfiguration backgroundSessionConfiguration:label];
conf.allowsCellularAccess = NO;
_urlSession = [NSURLSession sessionWithConfiguration:conf delegate:self delegateQueue:self.urlSessionQueue];
_urlSession.sessionDescription = @"Upload log files";
}
return _urlSession;
}
アップロードタスクの方法:
- (NSURLSessionUploadTask *)uploadTaskForFilePath:(NSStringの*)filePathにセッション:(NSURLSession *)セッション{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
// Consruct request:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
NSString *finalUrlString = [self.uploadURL absoluteString];
if (self.uploadUserId) {
[request setValue:self.uploadUserId forHTTPHeaderField:@"X-User-Id"];
finalUrlString = [finalUrlString stringByAppendingFormat:@"?id=%@", self.uploadUserId];
}
[request setURL:[NSURL URLWithString:finalUrlString]];
/*
It looks like this (it only works if you quote the filename):
Content-Disposition: attachment; filename="fname.ext"
*/
NSString *cdh = [NSString stringWithFormat:@"attachment; filename=\"%@\"", [filePath lastPathComponent]];
[request setValue:cdh forHTTPHeaderField:@"Content-Disposition"];
error = nil;
unsigned long fileSize = [[fm attributesOfItemAtPath:filePath error:&error] fileSize];
if (!error) {
NSString *sizeInBytesAsString = [NSString stringWithFormat:@"%lu", fileSize];
[request setValue:sizeInBytesAsString forHTTPHeaderField:@"X-Content-Length"];
}
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:fileUrl];
uploadTask.taskDescription = filePath;
return uploadTask;
}
アップロード機能:
[self.urlSession getTasksWithCompletionHandler:^ (にNSArray * dataTasks、NSArrayの* uploadTasks、NSArrayの* downloadTasks){
NSMutableDictionary *tasks = [NSMutableDictionary new];
int resumed_running_count = 0;
int resumed_not_running_count = 0;
int new_count = 0;
// 1/2. Resume scheduled tasks:
for(NSURLSessionUploadTask *task in uploadTasks) {
//MILogInfo(@"Restored upload task %zu for %@", (unsigned long)task.taskIdentifier, task.originalRequest.URL);
if (task.taskDescription) {
[tasks setObject:task forKey:task.taskDescription];
}
BOOL isRunning = (task.state == NSURLSessionTaskStateRunning);
if (!isRunning) {
resumed_not_running_count++;
}else{
resumed_running_count++;
}
[task resume];
}
// 2/2. Add tasks/files not scheduled yet:
NSString *uploadFilePath = nil;
// already uploading:
if (![tasks valueForKey:uploadFilePath]) {
NSURLSessionUploadTask *uploadTask = [self uploadTaskForFilePath:uploadFilePath session:_urlSession];
new_count++;
[uploadTask resume];
}
}];
(ボイド)アプリケーション:(のUIApplication *)アプリケーションhandleEventsForBackgroundURLSession:(NSStringの*)識別子completionHandler:(ボイド(^)())completionHandler {
背景セッションはUIApplecationデリゲート(AppDelegateコールバックを実装必要NSLog(@"Background URL session needs events handled: %@", identifier);
completionHandler();
}
あなたがUPLOを探しています新しいiPhone 3GSで撮影した広告動画や、端末にローカルに保存されている動画は何ですか? iPhoneのグローバルファイルシステムにアクセスする方法はないので、後者のオプションは実現できません。 – zpesk