title: Most effective method for video as background in iOS
body: <p>Perhaps you have noticed one of the latest trend in iOS-apps: Using videos as backgrounds - mainly at login- or "first launch" screens. Yesterday I attempted to mimic this with a very simple test project (only one view controller) and I am pleased with the results except for the <strong>performance</strong>. When trying it out in the iOS Simulator (on a simulated iPhone 6) the <strong>CPU usage</strong> fluctuates between <strong>70-110%</strong>. This seems very unreasonable for a simple login-screen.</p>
  
  <p>This is what it looks like in action:
  <a href="http://oi57.tinypic.com/nqqntv.jpg">http://oi57.tinypic.com/nqqntv.jpg</a></p>
  
  <p>The question is then: <em>Is there a more CPU-effective way to achieve this? How are the apps like Vine, Spotify and Instagram doing this?</em></p>
  
  <p>Before you answer; the method I used was a full-HD video played back using MPMoviePlayerController:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      // find movie file
      NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"arenaVideo" ofType:@"mp4"];
      NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
  
      // load movie
      self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
      self.moviePlayer.controlStyle = MPMovieControlStyleNone;
      self.moviePlayer.view.frame = self.view.frame;
      self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
      [self.view addSubview:self.moviePlayer.view];
      [self.view sendSubviewToBack:self.moviePlayer.view];
      [self.moviePlayer play];
  
      // loop movie
      [[NSNotificationCenter defaultCenter] addObserver: self
                                               selector: @selector(replayMovie:)
                                                   name: MPMoviePlayerPlaybackDidFinishNotification
                                                 object: self.moviePlayer];
  }
  
  #pragma mark - Helper methods
  
  -(void)replayMovie:(NSNotification *)notification
  {
      [self.moviePlayer play];
  }
  </code></pre>
  
  <p>Of course the edges of the video could have been trimmed so that the resolution would be something more along the lines of say 700x1080 instead of 1920x1080 but would that have made a huge difference in performance? Or should I compress the video with a certain format and settings to achieve optimal performance? Maybe there is an entirely alternate approach to this?</p>
  
  <p>Actually I tried using GIFs as described in this article: <a href="https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2">https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2</a></p>
  
  <p>The problem with that is:</p>
  
  <ul>
  <li>Creating GIFs out of videos takes a lot of time and effort</li>
  <li>I saw no significant decrease of CPU usage when I tried it</li>
  <li>Supporting multiple screen sizes is a total pain with this approach (at least when I tried - with Autolayout and Size Classes enabled - I couldn't get the GIF to scale correctly across devices)</li>
  <li>Quality of video is poor</li>
  </ul>