添加视频背景视图的最佳做法是什么?

时间:2023-01-20 21:44:58

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.

我想添加视频(或gif)的背景视图,如应用程序“Uber”我想在我的应用程序中使用视频背景视图很长一段时间。

And I want to know the answers to these questions:

我想知道这些问题的答案:

  • What of them will consume less battery energy
  • 它们会消耗更少的电池能量

  • Can I use it on iPhone or iPad
  • 我可以在iPhone或iPad上使用它吗?

  • Performance of this method
  • 这种方法的表现

Screenshot of the registration form

注册表单的屏幕截图

添加视频背景视图的最佳做法是什么?

6 个解决方案

#1


7  

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:

您有两个主要选择:使用带有GIF的imageView或使用AVPlayer或MPMoviePlayerController的背景视频。你可以找到两种方式的例子,这里有几个:

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.

通常使用视频:用户vikingosegundo视频通常会对其进行优化并且其编解码器处理GPU;显示GIF应该是唯一的“CPU工作”,因为它只显示一个帧循环。因此,能量比较是在简单的帧循环(GIF)和更复杂的帧循环之间,可以通过多种方式加速(视频)。然而,根据我的经验,小GIF无论如何都能提供良好的表现。

Can I use it on iPhone or iPad

我可以在iPhone或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.

在这两种情况下都很出色,但您必须小心GIF或视频的大小:您的文件(GIF或视频)越大,性能就越差。对于视频更准确地说,质量越高,性能就越差,而视频的持续时间不应该受到影响。

#2


4  

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.
  3. AMLoginViewController使用GPUImage过滤视频。

May this help lot.

愿这有用了。

#3


0  

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

您可以使用大量开源组件来实现此目的。

For example, this one supports GIF, movie playback

例如,这个支持GIF,电影播放

https://github.com/kaiinui/KIChameleonView

Or this for GIF playback

或者这用于GIF播放

https://github.com/liyong03/YLGIFImage

#4


0  

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

您可以直接使用视频并播放重复模式,

OR

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

您可以使用图像以及UIImageView类的默认animationImages属性。

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.

希望这对你有所帮助。

#5


0  

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

您可以使用.png或.jpeg格式的图像集来实现此目的,并将图像分配给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];

#6


0  

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

我使用了xCode 7和iOS 9.我使用了gif图像,它可以正常工作。

    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];

#1


7  

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:

您有两个主要选择:使用带有GIF的imageView或使用AVPlayer或MPMoviePlayerController的背景视频。你可以找到两种方式的例子,这里有几个:

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.

通常使用视频:用户vikingosegundo视频通常会对其进行优化并且其编解码器处理GPU;显示GIF应该是唯一的“CPU工作”,因为它只显示一个帧循环。因此,能量比较是在简单的帧循环(GIF)和更复杂的帧循环之间,可以通过多种方式加速(视频)。然而,根据我的经验,小GIF无论如何都能提供良好的表现。

Can I use it on iPhone or iPad

我可以在iPhone或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.

在这两种情况下都很出色,但您必须小心GIF或视频的大小:您的文件(GIF或视频)越大,性能就越差。对于视频更准确地说,质量越高,性能就越差,而视频的持续时间不应该受到影响。

#2


4  

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.
  3. AMLoginViewController使用GPUImage过滤视频。

May this help lot.

愿这有用了。

#3


0  

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

您可以使用大量开源组件来实现此目的。

For example, this one supports GIF, movie playback

例如,这个支持GIF,电影播放

https://github.com/kaiinui/KIChameleonView

Or this for GIF playback

或者这用于GIF播放

https://github.com/liyong03/YLGIFImage

#4


0  

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

您可以直接使用视频并播放重复模式,

OR

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

您可以使用图像以及UIImageView类的默认animationImages属性。

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.

希望这对你有所帮助。

#5


0  

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

您可以使用.png或.jpeg格式的图像集来实现此目的,并将图像分配给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];

#6


0  

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

我使用了xCode 7和iOS 9.我使用了gif图像,它可以正常工作。

    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];