Xcode iPhone SDK - 在应用程序启动时显示 gif 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10538328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:16:36  来源:igfitidea点击:

Xcode iPhone SDK - Show gif image on app Launch

xcodeios5gif

提问by Aleksander Azizi

I currently have a mp4 video converted from a gif playing at app launch, but using a video stops playing music and has problems with airplay devices connected.

我目前有一个从应用程序启动时播放的 gif 转换而来的 mp4 视频,但使用视频会停止播放音乐并且连接的播放设备出现问题。

Anyways what i really want is showing my gif on app launch. But i can't seem to get the gif to "play" it only shows one frame, how do i make it play the whole gif ?

无论如何,我真正想要的是在应用程序启动时显示我的 gif。但我似乎无法让 gif“播放”它只显示一帧,我如何让它播放整个 gif?

My current code:

我目前的代码:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Show Animation;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Launch" ofType:@"mp4"]];
LaunchPlayer = [[MPMoviePlayerViewController alloc] 
                                                 initWithContentURL:url];
LaunchPlayer.view.frame = self.view.bounds;
LaunchPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
LaunchPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
LaunchPlayer.view.tag = 1;
[self.view addSubview:LaunchPlayer.view];
[LaunchPlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(LaunchFinish) 
                                             name:@"MPMoviePlayerPlaybackDidFinishNotification"
                                           object:nil];
}

回答by Mick MacCallum

Although I don't see how the code you posted has anything to do with using a .gif, you its sticking to the first frame because iOS will not run an animated .gif. However, you can export each frame of your animation as a separate image and animate them like this.

虽然我看不出您发布的代码与使用 .gif 有什么关系,但您仍然坚持使用第一帧,因为 iOS 不会运行动画 .gif。但是,您可以将动画的每一帧导出为单独的图像,并像这样为它们设置动画。

-(void) viewDidLoad
{

imageView.animationImages = [NSArray arrayWithObjects:    
                                    [UIImage imageNamed:@"myImageFrame1.gif"],
                                    [UIImage imageNamed:@"myImageFrame2.gif"],
                                    [UIImage imageNamed:@"myImageFrame3.gif"],
                                    [UIImage imageNamed:@"myImageFrame4.gif"], nil];


    imageView.animationDuration = 2.0; 
    imageView.animationRepeatCount = 0; 
    [imageView startAnimating]; 

}

回答by Matosha

In my little example here. i used a UIWEBVIEW called webView and had spinning-loader.gif added as a resource. The image can me put anywhere in the project as long as its added. This showed the gif as well as made it animate. Hope this helps.

在我这里的小例子中。我使用了一个名为 webView 的 UIWEBVIEW,并将 spin-loader.gif 添加为资源。只要添加了图像,我就可以将其放在项目中的任何位置。这显示了 gif 并使其具有动画效果。希望这可以帮助。

NSString *pathImg = [[NSBundle mainBundle] pathForResource:@"spinning-loader" ofType:@"gif"];
NSString* webViewContent = [NSString stringWithFormat:
                            @"<html><body><img style='width:85;height:85;' src=\"file://%@\" /></body></html>", pathImg];
[webView loadHTMLString:webViewContent baseURL:nil];

webView.scrollView.bounces = NO;
webView.scrollView.scrollEnabled = NO;
webView.opaque=FALSE;
[webView setBackgroundColor:[UIColor clearColor]];