2014-03-31 2 views
0

사진을 찍어서 캡처하고 uiimage보기로 표시하려고하거나 동영상을 캡처하여 영화관 컨트롤러에서 다시 재생하려고합니다.iOS 기기에서 사진 및 동영상 찍기

나는 단추를 클릭 할 수 있으며, 내 uiimageview에 잘 표시되는 "사진 찍기"가 가능합니다. 그러나 "Take Video"버튼을 클릭하면 uiimageview에서 이미지가 사라집니다. Take 비디오는 작동하고 화면은 괜찮지 만 내 uiimage는 이제 사라졌습니다.

내가 다시 찍은 사진을 찍으면 내가 캡처 한 비디오가 사라 집니까?

.H 파일

#import <UIKit/UIKit.h> 
#import <MediaPlayer/MediaPlayer.h> 
#import <MobileCoreServices/MobileCoreServices.h> 

@interface addEventViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{ 

    UIImagePickerController *imagePicker; 
    UIImage *eventPhoto; 
    IBOutlet UIImageView *imageView; 

} 

-(IBAction)TakePhoto; 
- (IBAction)takeVideo:(id)sender; 
@property (copy, nonatomic) NSURL *movieURL; 
@property (strong, nonatomic) MPMoviePlayerController *movieController; 



@end 

하는 .m 파일

#import "addEventViewController.h" 

@interface addEventViewController(){ 

} 

@end 

@implementation addEventViewController 


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

    // FOR THE TAKE PHOTO 
    eventPhoto = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    [imageView setImage: eventPhoto]; 
    [self dismissViewControllerAnimated:YES completion:NULL]; 

    // FOR THE TAKE VIDEO 
    self.movieURL = info[UIImagePickerControllerMediaURL]; 
    [picker dismissViewControllerAnimated:YES completion:NULL]; 


} 


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

    //FOR THE TAKE PHOTO 
    [self dismissViewControllerAnimated:YES completion:NULL]; 

    // FOR THE TAKE VIDEO 
    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

// TAKE PHOTO BUTTON 
-(IBAction)TakePhoto{ 
    imagePicker = [[UIImagePickerController alloc] init]; 
    imagePicker.delegate = self; 
    [imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController: imagePicker animated:YES completion:NULL]; 


} 

// TAKE VIDEO BUTTON 
- (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 setContentURL:self.movieURL]; 
    [self.movieController.view setFrame:CGRectMake (420, 76, 320, 200)]; 
    [self.view addSubview:self.movieController.view]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:self.movieController]; 

    [self.movieController play]; 

} 

- (void)moviePlayBackDidFinish:(NSNotification *)notification { 

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



} 



- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 



@end 

답변

1

실제로 컴파일하고 코드를 실행하지만, 그것을보고, 당신은 클래스 인스턴스의 imagePicker를 정의하고하지 않은, TakePhoto 메서드 내에서 picker을 정의하는 반면 내 생각 엔 프로세스를 실행할 때 메소드 인스턴스가 완료되면 파괴된다는 것입니다.

대신 클래스 수준에서 선언 된 단일 imagePicker 인스턴스를 사용하고 사용자가 이미지 또는 비디오를로드하는지 여부에 따라 속성을 다르게 설정하고 이에 따라 선택한 미디어를 설정하십시오.