Singh
Singh

Reputation: 2161

calculate size of video in imagepickerdidFinishPickingMediaWithInfo in bytes

can we calculate the size of video recorded by user or picked by user from saved images folder inside the image picker delegate in bytes? thanks

Upvotes: 3

Views: 1276

Answers (1)

app_
app_

Reputation: 697

You can read the bytes size easily from a NSData object. So the only thing you have to do is create a NSData object from your video url.

Like this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];

    NSURL *imageURL = [info valueForKey:UIImagePickerControllerMediaURL];
    NSData *data = [NSData dataWithContentsOfURL:imageURL];
    NSLog(@"Total bytes %d", [data length]);
}

Swift:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    picker.dismissViewControllerAnimated(true, completion: nil)

    let imageUrl = info[UIImagePickerControllerMediaURL]
    let data = NSData(contentsOfURL: imageUrl)
    print("Total bytes \(data.length)")
}

Upvotes: 5

Related Questions