2009-06-30 5 views
7

के साथ वीडियो अपलोड करना क्या सर्वर पर वीडियो अपलोड करना संभव है? मुझे पता है कि छवियां संभव हैं। अगर कोई मुझे सही दिशा में इंगित कर सकता है तो यह शानदार होगा।आईफोन

धन्यवाद

+0

आप uplo के लिए देख रहे डिवाइस पर स्थानीय रूप से संग्रहीत नए आईफोन 3 जीएस या वीडियो द्वारा लिया गया विज्ञापन वीडियो ?? याद रखें कि आईफोन की वैश्विक फाइल सिस्टम तक पहुंचने का कोई तरीका नहीं है, इसलिए बाद वाला विकल्प संभव नहीं है। – zpesk

उत्तर

2

UIImagePickerController पर एक नज़र डालें। 3.0 के रूप में आप किसी वीडियो को शूट करने या मौजूदा वीडियो चुनने का विकल्प चुन सकते हैं। डॉक्स आप हालांकि फिल्म पर 10min अधिकतम करने के लिए सीमित कर रहे हैं के अनुसार:

http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

+0

कूल! बहुत बहुत धन्यवाद –

17

संपादित अगस्त 2015

इस उत्तर अब गंभीरता से पुराना हो चुका है। लिखने के समय वहां कई विकल्प नहीं थे और वीडियो आकार में अपेक्षाकृत छोटे थे। यदि आप इसे अभी देख रहे हैं, तो मैं AFNetworking का उपयोग करूंगा जो इसे बहुत आसान बनाता है। यह सभी को मेमोरी में रखने के बजाय फ़ाइल से अपलोड स्ट्रीम करेगा, और नए ऐप्पल पृष्ठभूमि अपलोड टास्क का भी समर्थन करेगा। यहाँ

डॉक्स: 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 पोस्ट अनुरोध बनाता है और प्रस्तुत यह। यदि आप सभ्य त्रुटि प्रबंधन करना चाहते हैं और एक लाइब्रेरी का उपयोग करने पर विचार करना चाहते हैं जो एसिंक अपलोड (github पर एक है)

पोर्ट पर ध्यान दें: ऊपर सर्वर यूआरएल पर 3000, मुझे बग परीक्षण के लिए यह आसान लगता है विकास मोड में अपने डिफ़ॉल्ट पोर्ट 3000 पर एक रेल सर्वर शुरू करने के लिए तो मैं

आशा डिबगिंग उद्देश्यों के लिए अनुरोध पैरामीटर देख सकते हैं इस में मदद करता है

+0

कूल, मैं इसे आज़माउंगा धन्यवाद दोस्त –

+0

कोई प्रॉब्स नहीं। एक बात ध्यान में रखना है कि वर्तमान एसडीके आपको पूर्ण रेज वीडियो तक पहुंच नहीं देता है क्योंकि यह आईट्यून्स के साथ समन्वयित होगा। मेरे परीक्षणों ने 480x360 का अधिकतम वीडियो आकार दिखाया। यदि आप चारों ओर एक काम पा सकते हैं तो कृपया हमें बताएं। धन्यवाद – ADAM

+0

क्या यह गायब कचरा संग्रह है? – jocull

0
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]); 
+1

आप यूआईमेजपीकर नियंत्रक द्वारा उपयोग की गई वीडियो फ़ाइल को उपयोग करके भेज सकते हैं यह कोड। और आपको जिथब से एसिहेटप्रक्वेट फ्रेमवर्क को पकड़ना होगा – chinni

3

के बाद से iOS8 3 पार्टी पुस्तकालयों और आप उपयोग करना कोई जरूरत नहीं है सीधे वीडियो से स्ट्रीम वीडियो स्ट्रीम कर सकते हैं जो सी हल करता है मेमोरी त्रुटि की rucial छोड़ना यदि आप करते हुए उन्हें फ़ाइल से लोड हो रहा है बड़ा वीडियो अपलोड करने का प्रयास करते समय:

// 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 कॉम्बो समाधान, जो न केवल की जाने आप फ़ाइल से सीधे स्ट्रीम, लेकिन प्रदान करता है आईओएस प्रक्रिया में कार्य भी निर्दिष्ट कर सकते हैं, जो आपके ऐप को बंद होने पर भी समाप्त होने के लिए अपलोड कर देगा। इसे थोड़ा और कोडिंग की आवश्यकता है और मेरे पास यह सब लिखने का कोई समय नहीं है (आप इसे Google कर सकते हैं)।

यहाँ सबसे महत्वपूर्ण हिस्से हैं:

  1. 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; 
    

    }

  2. अपलोड कार्य विधि:

    - (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; 
    

    }

  3. अपलोड समारोह:

    [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]; 
         } 
    
    }]; 
    
  4. पृष्ठभूमि सत्र की आवश्यकता है UIApplecation प्रतिनिधि (AppDelegate कॉलबैक लागू किया:

    • (शून्य) आवेदन: (UIApplication *) आवेदन handleEventsForBackgroundURLSession: (NSString *) पहचानकर्ता completionHandler: (शून्य (^)()) completionHandler {

      NSLog(@"Background URL session needs events handled: %@", identifier); 
      completionHandler(); 
      

      }