Frage

So this is my attempt:

I first create an "Image picker" which allows me to take the video:

- (IBAction)takeVideo:(UIButton *)sender {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    
    [self presentViewController:picker animated:YES completion:NULL];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    self.movieURL = info[UIImagePickerControllerMediaURL];
        [picker dismissViewControllerAnimated:YES completion:NULL];
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
}
- (void)viewDidAppear:(BOOL)animated {
    
    self.movieController = [[MPMoviePlayerController alloc] init];
    self.movieController.controlStyle = MPMovieControlStyleNone;
    [self.movieController setContentURL:self.movieURL];
    [self.movieController.view setFrame:CGRectMake (15, 125, 292, 390)];
    self.movieController.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.movieController.view];
    [self.movieController play];
    
    NSString *myString = [self.movieURL absoluteString];
    //THE DATA IS RETURNING 0 :
    NSData *videoData = [NSData dataWithContentsOfFile:myString];
    NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:videoData.length countStyle:NSByteCountFormatterCountStyleFile]);
    [self setImageDataToSend:videoData];

}

- (void)moviePlayBackDidFinish:(NSNotification *)notification {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    [self.movieController stop];
    [self.movieController.view removeFromSuperview];
    self.movieController = nil;
    
}

I am not receiving any data from the URL though?

I then try and upload it to my site:

-(IBAction)uploadvideotowebsite:(id)sender{
    //you can use any string instead "com.mycompany.myqueue"
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mcompany.myqueue", 0);
    //turn on
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    dispatch_async(backgroundQueue, ^{
    
    
    PFUser *user = [PFUser currentUser];
    NSString * url = [NSString stringWithFormat:@"http://******.co.uk/****/imageupload.php"];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSMutableData *body = [[NSMutableData alloc]init];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *String = @"Content-Disposition: form-data; name=\"image\"; filename=\"";
    NSString *String2 = @"video";
    NSString *String3 = @".MOV\"\r\n";
    NSString *connection1 = [String stringByAppendingString: String2];
    NSString *done = [connection1 stringByAppendingString: String3];
    [body appendData:[[NSString stringWithFormat:done] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: audio/basic\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:_imageDataToSend];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    //Send the Request
    NSData* returnData = [NSURLConnection sendSynchronousRequest: request
                                               returningResponse: nil error: nil];
    //serialize to JSON
    if (!returnData){
        return; // or handle no connection error here
    }else{
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
        
        //parsing JSON
        bool success = [result[@"success"] boolValue];
        if(success){
            NSLog(@"Success=%@",result[@"msg"]);
            //[self performSegueWithIdentifier:@"toshow" sender:self];
        }else{
            NSLog(@"Fail=%@",result[@"msg"]);
        }
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        //turn off
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    });
});


}

I am receiving no errors. But I am receiving a file of size 0kb on my site.

Can anybody explain why I am getting no data from the video?

War es hilfreich?

Lösung

Can anybody explain why I am getting no data from the video?

Sure, here's your mistake:

 NSString *myString = [self.movieURL absoluteString];

should be path

NSString *myString = [self.movieURL path];
NSData *videoData = [NSData dataWithContentsOfFile:myString];
NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:videoData.length countStyle:NSByteCountFormatterCountStyleFile]);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top