2016-07-30 57 views
2

我想上傳圖片和視頻在同一時間,波紋管編碼我試過但沒有上傳到服務器,我想要一個視頻的縮略圖,所以我會採取縮略圖圖像從花費一些時間之後相同的視頻和兩個圖像和視頻我要上傳到服務器我想上傳圖片和視頻在同一時間從UIImagePicker

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    { 

       NSLog(@"Image And Video will be uploaded"); 

       img = [info valueForKey:UIImagePickerControllerOriginalImage]; 

    NSURL *imurl=[info valueForKeyPath:UIImagePickerControllerMediaURL]; 
    UIImage *croppedImage = [appdel imageWithImage:img scaledToWidth:400]; 

      NSString *finalUniqueImageNAme1; 
      NSString *finalUniqueImageNAme2; 


       thumbimages = [self thumbnailImageFromURL:imurl]; 

       NSData *videoData = [NSData dataWithContentsOfURL:imurl]; 




       if(videoData.length) 
       { 

        NSDate *currentDate = [[NSDate alloc] init]; 
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 
        NSString *localDateString = [dateFormatter stringFromDate:currentDate]; 
        NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""]; 
        NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1]; 

       finalUniqueImageNAme1 = [cleanedString2 stringByAppendingString:@".mp4"]; 

       } 


       if(thumbimages != nil) 
       { 

        NSDate *currentDate = [[NSDate alloc] init]; 
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 
        NSString *localDateString = [dateFormatter stringFromDate:currentDate]; 
        NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""]; 
        NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1]; 



        finalUniqueImageNAme2 = [cleanedString2 stringByAppendingString:@".jpg"]; 


       } 
       NSData *imageData = UIImageJPEGRepresentation(thumbimages, 90); 
       NSString *urlString = @"http://vygears.com/tcdc-portfolio/Abdul/chat/upload/Vupload_file.php"; 
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
       [request setURL:[NSURL URLWithString:urlString]]; 
       [request setHTTPMethod:@"POST"]; 
       NSString *boundary = @"---------------------------14737809831466499882746641449"; 
       NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
       [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

       NSMutableData *body = [NSMutableData data]; 
       [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 



       [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file1\"; filename=\"%@\"\r\n",finalUniqueImageNAme1] dataUsingEncoding:NSUTF8StringEncoding]]; 
       [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


       [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file2\"; filename=\"%@\"\r\n",finalUniqueImageNAme2] dataUsingEncoding:NSUTF8StringEncoding]]; 
       [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 




        [body appendData:[NSData dataWithData:videoData]]; 

        [body appendData:[NSData dataWithData:imageData]]; 



       [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

       [request setHTTPBody:body]; 

       // NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

       // NSLog(@"Successfully uploaded"); 

       NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
       if(conn) 
       { 
        NSLog(@"Connection Successful"); 
        // [self dismissModalViewControllerAnimated:true]; 
        [self dismissViewControllerAnimated:YES completion:nil]; 
       } 
       else 
       { 
        NSLog(@"Connection could not be made"); 
       } 
      } 
     } 

     - (UIImage *)thumbnailImageFromURL:(NSURL *)videoURL { 

      AVURLAsset *asset = [[AVURLAsset alloc] initWithURL: videoURL options:nil]; 
      AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
      NSError *err = NULL; 
      CMTime requestedTime = CMTimeMake(12,13);  // To create thumbnail image 
      CGImageRef imgRef = [generator copyCGImageAtTime:requestedTime actualTime:NULL error:&err]; 
      NSLog(@"err = %@, imageRef = %@", err, imgRef); 
      thumbnailImage = [[UIImage alloc] initWithCGImage:imgRef]; 
      CGImageRelease(imgRef); // MUST release explicitly to avoid memory leak 
      return thumbnailImage; 
     } 

回答

2

最後,我已經拿到了代碼

img = [info valueForKey:UIImagePickerControllerOriginalImage]; 

NSURL *imurl=[info valueForKeyPath:UIImagePickerControllerMediaURL]; 
UIImage *croppedImage = [appdel imageWithImage:img scaledToWidth:400]; 
NSLog(@"Image And Video will be uploaded"); 

     NSString *finalUniqueImageNAme1; 
     NSString *finalUniqueImageNAme2; 
     NSData *videoData = [NSData dataWithContentsOfURL:imurl]; 
     thumbimages = [self thumbnailImageFromURL:imurl]; 


     if(videoData.length) 
     { 

      NSDate *currentDate = [[NSDate alloc] init]; 
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

      [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 
      NSString *localDateString = [dateFormatter stringFromDate:currentDate]; 
      NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""]; 
      NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1]; 

      finalUniqueImageNAme1 = [cleanedString2 stringByAppendingString:@".mp4"]; 

     } 


     if(thumbimages != nil) 
     { 

      NSDate *currentDate = [[NSDate alloc] init]; 
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

      [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 
      NSString *localDateString = [dateFormatter stringFromDate:currentDate]; 
      NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""]; 
      NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1]; 



      finalUniqueImageNAme2 = [cleanedString2 stringByAppendingString:@".jpg"]; 


     } 

     NSData *imageData = UIImageJPEGRepresentation(thumbimages, 90); 

     NSString *urlString = @"http://vygears.com/tcdc-portfolio/Abdul/chat/upload/Vupload_file.php"; 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 
     NSString *boundary = @"---------------------------14737809831466499882746641449"; 
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
     NSMutableData *body = [NSMutableData data]; 

     if (imageData) 
     { 


      //======= Image Data=======// 

      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file2\"; filename=\"%@\"\r\n",finalUniqueImageNAme2] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

      [body appendData:[NSData dataWithData:imageData]]; 
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

      //======= Video Data=======// 


      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file1\"; filename=\"%@\"\r\n",finalUniqueImageNAme1] dataUsingEncoding:NSUTF8StringEncoding]]; 
      [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

      [body appendData:[NSData dataWithData:videoData]]; 
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

     } 

     [request setHTTPBody:body]; 
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     if(conn) 
     { 
      NSLog(@"Connection Successful"); 
      // [self dismissModalViewControllerAnimated:true]; 
      [self dismissViewControllerAnimated:YES completion:nil]; 
     } 
     else 
     { 
      NSLog(@"Connection could not be made"); 
     } 
    } 
相關問題