我的应用中有一个ImageView,用户可以录制视频或从相机胶卷加载。如果我想在UIImageView中播放视频,我需要做什么。
她是代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) // Media is a video
{
NSURL* videoUrl = info[UIImagePickerControllerMediaURL];
_videoUrl = [[NSURL alloc] initWithString:[videoUrl absoluteString]];
[self loadVideo:_videoUrl];
}
}
- (void)loadVideo:(NSURL*)url
{
AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *one = [[UIImage alloc] initWithCGImage:oneRef];
[_imageView setImage:one];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
我正在使用以下代码播放视频,但它正在全屏播放视频。
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:_videoUrl];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
我需要播放视频,但在UIImageView中。有可能吗?
答案 0 :(得分:0)
你的最终代码应该是这样的。你忘了添加框架
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:_videoUrl];
moviePlayer.view.frame = _yourImageView.frame;
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];