ios - 使用 MPMoviePlayerController 时的内存泄漏

标签 ios objective-c memory-leaks mpmovieplayercontroller xcode5

在我的项目中,我在不使用 Storyboard的情况下手动创建 View 。我尝试在点击图像时播放视频。它工作正常。但是每次当我点击图像时检查它显示内存泄漏,我已经搜索了很多并应用但没有用。 在我的 Appdelegate.h 文件中:

@property (strong, nonatomic) MPMoviePlayerController *theMoviePlayer;
@property (strong, nonatomic) UIImageView *image1;

在 .m 文件中:

-(void) startPage{
.....
_image1 = [[UIImageView alloc] initWithFrame:CGRectMake((self.window.frame.size.width/2)-25, 40, 50, 50)];
[_image1 setUserInteractionEnabled:YES];
_image1.image = [UIImage imageNamed:@"image_2.jpg"];
_tapImage1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(image1Tapped:)];
[_image1 addGestureRecognizer:_tapImage1];
.....}

在 imageTapped() 中,

-(void) image1Tapped:(UITapGestureRecognizer *)sender
{
  .....

 [_image1 removeFromSuperview];

 _theMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];


    [_theMoviePlayer setControlStyle:MPMovieControlStyleFullscreen];

    [_theMoviePlayer.view setFrame:CGRectMake(0,-55, self.window.frame.size.width, self.window.frame.size.height)];



    [_theMoviePlayer setScalingMode:MPMovieScalingModeAspectFill];

    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];

    [backgroundWindow addSubview:_theMoviePlayer.view];

    [_theMoviePlayer.view bringSubviewToFront:backgroundWindow];
[_theMoviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinished:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification object:_theMoviePlayer];
...}

每次进入 imageTapped: 方法时我都会发生内存泄漏。任何帮助将不胜感激。

最佳答案

@property (strong, nonatomic) MPMoviePlayerController *theMoviePlayer;
@property (weak, nonatomic) UIImageView *image1;

另一个想法是你的 theMoviePlayer 没有被移除,试着让它的 View 透明,看看是否已经在新的后面工作了

关于ios - 使用 MPMoviePlayerController 时的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19515884/

相关文章:

ios - 图表 LineChartView 框架获取 : fatal error: unexpectedly found nil while unwrapping an Optional value

ios - 为什么在将 UICollectionViewLayout 分配给 UICollectionView 后,UICollectionViewCells 不再可见?

ios - 为什么这个 UIWebView 到 UIImage 的代码会呈现一个空白图像?

IOS/objective-C : Case Insenstive Search using StringByReplacingOccurenceOfString

ios - Xcode dataSource 必须从 tableView :cellForRowAtIndexPath 返回一个单元格

ios - Watchkit-应用程序 : handleWatchKitExtensionRequest error

Python - 解决内存泄漏问题

jar和debug之间的java内存泄漏区别

java - Logger引起的内存泄漏

ios - Core Plot 中的 expandRangeByFactor 方法出错