2014-03-31 59 views
0

我試圖能夠拍攝並拍攝照片並在uiimage視圖中顯示和/或拍攝視頻並在影片播放器控制器中播放。拍攝iOS設備上的照片和視頻

我可以點擊一個按鈕和「拍照」,它顯示在我的uiimageview中就好了。然而,如果我點擊一個按鈕來「拍攝視頻」,圖像就會從uiimageview中消失。拍攝視頻的工作原理和顯示效果很好,但就像我說的那樣,我的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實例,它的性質不同,這取決於用戶是否被加載的圖像或視頻設置,並相應地設置所選擇的媒體。