// CameraViewController.h
@interface CameraViewController : UIViewController{
UIImagePickerController *_picker;
}
@property(强,非原子)IBOutlet UIImageView *imageView;
@property (nonatomic, 保留) MPMoviePlayerController *player;
- (IBAction)loadImage:(UIButton *)sender;
// CameraViewController.m
@interface CameraViewController ()
@property(强,非原子) MPMoviePlayerController *playView;
@结尾
@implementation CameraViewController
@synthesize imageView = _imageView;
@synthesize 播放器 = _player;
@synthesize playView = _playView;
- (void)viewDidLoad
{
[super viewDidLoad];
_picker = [[UIImagePickerController alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
(IBAction)loadImage:(UIButton *)sender {
_picker.delegate = self;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSArray *mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie, nil];
_picker.mediaTypes = 媒体类型;
[自我presentViewController:_picker动画:YES完成:NULL];
}
杂注标记 -ImagePicker 代表
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = nil;
NSURL *imageURL = nil;
imageURL = [信息 valueForKey:UIImagePickerControllerMediaURL];
if (imageURL == nil) {
image = [info valueForKey:UIImagePickerControllerEditedImage]; if (image == nil) { image = [info valueForKey:UIImagePickerControllerOriginalImage]; _imageView.image = image; } else { }
}
别的 {
self.player = [[MPMoviePlayerController alloc] initWithContentURL:imageURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player]; self.player.view.frame = CGRectMake(10.0, 10.0, 200.0, 200.0); self.player.view.backgroundColor = [UIColor clearColor]; self.player.movieSourceType = MPMovieSourceTypeFile; [self.player prepareToPlay]; [self.view addSubview:self.player.view]; [self.player play];
}
[选择器dismissViewControllerAnimated:YES完成:NULL];
}
(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[选择器dismissViewControllerAnimated:YES完成:NULL];
}
杂注标记 - 用户定义
(void)movieFinishedCallback:(NSNotification *)notification {
MPMoviePlayerViewController *player = [通知对象];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player.view removeFromSuperview];
}
movieFinishedCallback: 方法会立即被调用,所以视频不能完全播放。
如何解决?