Search code examples
iosobjective-cuiwebviewgif

Add a gif video file in UIWebview


enter image description here

enter image description here

I'm adding a GIF image in UIWbeview in Objective-C. The problem is that the image size doesn't fit in the screen and the contents above the gif video is not showing. How can I adjust the image size and show the buttons above it.

My code is:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"back" ofType:@"gif"];
    NSData *gif = [NSData dataWithContentsOfFile:filePath];
    
    UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
    [webViewBG loadData:gif MIMEType:@"gif" textEncodingName:NULL baseURL:NULL];
    webViewBG.userInteractionEnabled = NO;
    webViewBG.scalesPageToFit=YES;
    [self.view addSubview:webViewBG];
    
    // Do any additional setup after loading the view.
}

Solution

  • You can move your code to viewDidAppear

     - (void)viewDidAppear:(BOOL)animated  {
    
        [super viewDidAppear:animated];
    
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"back" ofType:@"gif"];
        NSData *gif = [NSData dataWithContentsOfFile:filePath];
    
        UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
        [webViewBG loadData:gif MIMEType:@"image/gif" textEncodingName:@"UTF-8" baseURL:NULL];
        webViewBG.userInteractionEnabled = NO;
        webViewBG.scalesPageToFit=YES;
        [self.view addSubview:webViewBG];
    }