I am new to coding the iOS platform and goal C in general. I am writing a simple iPhone application that uses a storyboard with a high png file displayed through UIImageView embedded in UIScrollView and next to it a button that will play the movie.
My problem is that when the movie ends / exits and returns to the original screen, scrolling in UIScrollView does not work. I nailed the "reason". This happens when I add an MPMoviePlayerViewController object.view object to a subview. But I'm not sure how to fix this problem. Here is my distilled code:
.h file
@interface StuffViewController : UIViewController @property (strong,nonatomic) IBOutlet UIImageView *imageView; @property (strong,nonatomic) IBOutlet UIScrollView *scrollView; -(IBAction) playMovie; -(void) moviePlayBackDidFinish:(NSNotification*)notification; @end
.m file
-(void) viewDidLoad { self.imageView.image = [UIImage imageNamed:@"image.png"]; } -(void) viewDidAppear:(BOOL)animated { self.scrollView.contentSize = self.imageView.image.size; } -(void) playMovie { NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"movieTitle" ofType:@"mp4"]]; MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; [moviePlayer setMovieSourceType:MPMovieSourceTypeFile]; [[self view] addSubview:moviePlayer.view];
I identified the culprit by commenting on the sections, and, as I said, scrolling βblocksβ in β[[view] viewSubview: moviePlayer.view]; and doesnβt recover until I switch to another view and then return.
Any help on this would be greatly appreciated.
EDIT: I found an interesting wrinkle that can help detect the underlying problem.
I have been using MPMoviePlayerController all this time. However, when switching to MPMoviePlayerViewController , some interesting things happened.
Here is the modified - (void) playMovie
-(void) playMovie { self.scrollView.contentOffset = CGPointZero; NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:totalTitle ofType:@"mp4"]]; MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; [self presentMoviePlayerViewControllerAnimated:playerController]; playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; [playerController.moviePlayer play]; playerController = nil; }
Interestingly, UIScrollView will still work, HOWEVER, if it was scrolled down, it would no longer be able to scroll up where it was when the movie was launched. I fixed this by adding self.scrollView.contentOffset = CGPointZero at the beginning of playMovie to tell scrollView to go to the beginning (so that there would be nothing above it to scroll back). I assume that adding some if statement to the code in viewDidAppear that would keep scrollView.contentSize from being re-executed could solve the problem of being unable to scroll through the backup, but I like the "cleanliness" of running it back to the top.
One last question. Using MPMoviePlayerViewController like this caused some interesting errors to appear in my debugger when the line MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; . They look like this:
Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextSaveGState: invalid context 0x0 Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextClipToRect: invalid context 0x0 Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextTranslateCTM: invalid context 0x0 Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextDrawShading: invalid context 0x0 Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextRestoreGState: invalid context 0x0
It seems he did not break anything. However, I tend to be a perfectionist when it comes to erroneous allegations of errors. I did some research on these errors, but I did not find anything suitable that could have a strong hand in this situation.
Thanks for the help! Once again, I would appreciate any help with this.