【问题标题】:Capture photo and record video on same view在同一视图上拍摄照片和录制视频
【发布时间】:2017-02-08 06:25:01
【问题描述】:

我无法弄清楚如何在同一视图上拍摄照片和录制视频,因为两者使用的方法相同。 我试过了,但仍然不清楚代码是否正确。

如果有人能检查代码并告诉我它是否正确,我将不胜感激。

下面是代码:

 - (IBAction)CaptureRubbish:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)RecordRubbish:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
        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)videoPlayBackDidFinish:(NSNotification *)notification {

    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    // Stop the video player and remove it from view
    [self.videoController stop];
    [self.videoController.view removeFromSuperview];
    self.videoController = nil;

    // Display a message
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Video Playback" message:@"Just finished the video playback. The video is now removed." preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okayAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okayAction];
    [self presentViewController:alertController animated:YES completion:nil];

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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self->ImageVIew.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];

    self.videoURL = info[UIImagePickerControllerMediaURL];
    [picker dismissViewControllerAnimated:YES completion:NULL];

    self.videoController = [[MPMoviePlayerController alloc] init];

    [self.videoController setContentURL:self.videoURL];
    [self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, 460)];
    [self.view addSubview:self.videoController.view];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(videoPlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:self.videoController];
    [self.videoController play];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

【问题讨论】:

  • - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { } 在此方法中,您可以为照片和视频编写代码并设置一个 if 条件
  • @HimanshuMoradiya 我应该在 if-else 条件下放什么?

标签: ios objective-c xcode video-capture


【解决方案1】:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   if (info[UIImagePickerControllerMediaType] == (NSString *) kUTTypeImage) {
     // here your image capture code
   }
   else if(info[UIImagePickerControllerMediaType]== (NSString *)kUTTypeMovie)
   {
      // here your video capture code
   }
   [picker dismissViewControllerAnimated:YES completion:NULL];

}

【讨论】:

  • 这个目标c的答案
猜你喜欢
  • 1970-01-01
  • 2018-10-07
  • 2014-12-03
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 2018-01-02
  • 2012-11-06
  • 1970-01-01
相关资源
最近更新 更多