Tikhonov Aleksandr
Tikhonov Aleksandr

Reputation: 14319

What is the best practice add video background view?

I want to add background view with video (or gif) like in app "Uber" I'd like to use video background view for a long time in my app.

And I want to know the answers to these questions:

Screenshot of the registration form

enter image description here

Upvotes: 7

Views: 8695

Answers (6)

Ahmed Abdallah
Ahmed Abdallah

Reputation: 2355

I used xCode 7 and iOS 9. I used gif image and it work correctly.

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Name of your image" ofType:@"gif"];
    NSData *gif = [NSData dataWithContentsOfFile:filePath];
    UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
    [webViewBG loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    webViewBG.userInteractionEnabled = NO;
    [self.view addSubview:webViewBG];

Upvotes: 0

Massimo Polimeni
Massimo Polimeni

Reputation: 4906

First of all,

you have two main choices: use a imageView with a GIF or use a video for background with AVPlayer or MPMoviePlayerController. You can find a lot of example for both ways, here's a few:

In reply to your questions:

What of them will consume less battery energy

Generally use a video: as noted by the user vikingosegundo video are usually optimized and their codecs deal with GPUs; displaying a GIF should be a only "CPU-job" because it's just show a loop of frames. So the energy comparison is between a simple loop of frames (GIF) and a more complex frames loop that can be accelerated in many ways (video). In my experience however a small GIF give a good performance anyway.

Can I use it on iPhone or iPad

In both but you have to be careful with aspect ratio and autolayout constraints

Performance of this method

Excellent in both cases but you have to be careful about the size of your GIF or video: bigger is your file (GIF or video), worse should be the performance. For video more precisely, higher is the quality worse should be the performance while the duration of the video shouldn't be impact.

Upvotes: 7

Nimit Parekh
Nimit Parekh

Reputation: 16864

One of the popular and open source library for Background video play library sample source code download from below link

  1. VideoCover-iOS-Demo
  2. AMLoginViewController Uses GPUImage For Filter videos.

May this help lot.

Upvotes: 4

nilam_mande
nilam_mande

Reputation: 931

You can achieve this using the set of images in .png or .jpeg format and assign images to the UIImageView:

UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 548)];

//Add images which will be used for the animation using an array. Here I have created an array on the fly
NSMutableArray *drawingImgs = [NSMutableArray array];
[drawingImgs addObject:[UIImage imageNamed:@"image1.png"]];
[drawingImgs addObject:[UIImage imageNamed:@"image2.png"]];
[drawingImgs addObject:[UIImage imageNamed:@"image3.png"]];
[drawingImgs addObject:[UIImage imageNamed:@"image4.png"]];
[drawingImgs addObject:[UIImage imageNamed:@"image5.png"]];

backgroundImageView.animationImages = drawingImgs;
//Set the duration of the entire animation
backgroundImageView.animationDuration = 0.5;

//Set the repeat count. If you don't set that value, by default will be a loop (infinite)
backgroundImageView.animationRepeatCount = 1;

//Start the animationrepeatcount
[backgroundImageView startAnimating];

Upvotes: 0

Nilesh Patel
Nilesh Patel

Reputation: 6394

You can use the video directly & Play it repeat mode,

OR

You can use images along with default animationImages property of UIImageView Class.

Like Below,

 YourImageView.animationImages = [NSArray arrayWithObjects:  
                                   [UIImage imageNamed:@"sample_1.png"],
                                   [UIImage imageNamed:@"sample_2.png"],
                                   [UIImage imageNamed:@"sample_3.png"],
                                   [UIImage imageNamed:@"sample_4.png"],
                                   [UIImage imageNamed:@"sample_5.png"],
                                   nil];

 // How many seconds it should take to go through all images one time.
 YourImageView.animationDuration = 5.0;

 // How many times to repeat the animation (0 for indefinitely).
 YourImageView.animationRepeatCount = 4;

 [YourImageView startAnimating];

Hope this helps you.

Upvotes: 0

Alexander Tkachenko
Alexander Tkachenko

Reputation: 3281

You can use a lot of open-source components for this purpose.

For example, this one supports GIF, movie playback

https://github.com/kaiinui/KIChameleonView

Or this for GIF playback

https://github.com/liyong03/YLGIFImage

Upvotes: 0

Related Questions