Search code examples
iosobjective-curlnsdata

data from camera video returning zero


this is my code for getting a video from UIImagePickerController :

- (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)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];
    NSData *videoData = [NSData dataWithContentsOfFile:myString];
    NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:videoData.length countStyle:NSByteCountFormatterCountStyleFile]);
    [self setImageDataToSend:videoData];

}

where:

NSString *myString = [self.movieURL absoluteString];
        NSData *videoData = [NSData dataWithContentsOfFile:myString];
        NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:videoData.length countStyle:NSByteCountFormatterCountStyleFile]);
        [self setImageDataToSend:videoData];

Is returning zero? How come when it obviously is showing data?


Solution

  • Get the size like this

    NSError *error = nil;
    NSDictionary *attributes = [[NSFileManager defaultManager]
                                attributesOfItemAtPath:[self.movieURL path] error:&error];
    
    if (!error) {
        NSLog(@" size= %@",[attributes objectForKey:NSFileSize]);
    }else{
        NSLog(@"err= %@",error);
    }
    

    if you want KB

    NSString *myString = [self.movieURL path];
    NSData *videoData = [NSData dataWithContentsOfFile:myString];
    NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:videoData.length countStyle:NSByteCountFormatterCountStyleFile]);