iOS - Playing video through UIWebView: Player controls appear in awkward position - ios

I'm trying to play a .m3u8 through a webView inside a HTML video tag.
The video plays correctly and has the functionality I need. But the presentation is off... The player controls appear on the bottom of the video, overlayed half-way on and half-way off the video content.
(Sorry, massive screenshots)
Here, I extended the frame for the webview, and you can see that the video stops, but the controls appear lower.
Does anyone know if there is a way to reposition the player's controls?
Here's my webview code:
- (void)viewDidAppear:(BOOL)animated {
_webView.delegate = self;
_webView.allowsInlineMediaPlayback = YES;
NSString *urlAddress = #"http://www.pulpwoodpress.com/playVidTest.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
_webView.scrollView.scrollEnabled = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
NSLog(#"%f, %f", theWebView.scrollView.contentSize.width, theWebView.scrollView.contentSize.height);
CGSize contentSize = CGSizeMake(theWebView.scrollView.contentSize.width, theWebView.scrollView.contentSize.height);
CGSize viewSize = self.view.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
theWebView.scrollView.contentOffset = CGPointMake(0, 17);
}

Related

Can't seem to get video height for AVPlayerLayer

I can't seem to get the height of the video from the URL I have. Every time I log the playerLayer.videoRect.bounds.size.the height returns 0.000. Any help would be appreciated on how to adjust the playerLayer's height based on the video.
I have the playerLayer's height set to half the height of the view just to get the video to show up.
Here is my edited code:
NSURL *videoURL = [NSURL URLWithString:self.videoURL];
self.player = [AVPlayer playerWithURL:videoURL];
AVAssetTrack* track = [[AVAsset assetWithURL:videoURL] tracksWithMediaType:AVMediaTypeVideo].firstObject;
CGSize size = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform);
CGFloat videoWidth = size.width;
CGFloat videoHeight = size.height;
NSLog(#"%f wide, %f high)", videoWidth, videoHeight);
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.frame = CGRectMake(0, self.navigationController.navigationBar.bottom, self.view.bounds.size.width, videoHeight);
[self.view.layer addSublayer:self.playerLayer];
[self.player play];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
You can use AvAssetTrack to get size of video.
NSString* urlString = #"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
NSURL* url = [[NSURL alloc]initWithString:urlString];
AVAssetTrack* track = [[AVAsset assetWithURL:url] tracksWithMediaType:AVMediaTypeVideo].firstObject;
CGSize size = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform);
CGFloat width = size.width; // 640
CGFloat height = size.height; // 360
Note: You should put it in background mode because it will block main thread until finish get size success.
There are two problems:
You are asking for this information too soon. Like everything else about video, gathering the video's size takes time.
You have the dependency order backwards. The videoRect depends upon the layer's bounds. But you have not given it any bounds; it has zero size.

How to center a small size svg icon in a larger size webview?

I am using a Webview to load svg icons in my app. It works well with big images by using scalesPageToFit property. Now I am facing issues when using small size icons to fit in webview frame.
e.g. -
WebView size - 150w*150h
Icon dimension - 30*30
In this situation, icons align to the left top position of the webview. Is there any suggestion to center this in the web view or to enlarge it within webview frame? Any suggestion?
NSURL *imageURL = [NSURL URLWithString:#"...badge.svg"];
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
[self.menuView.webView loadRequest:request];
self.menuView.webView.delegate = self;
//Delegate method
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize contentSize = webView.scrollView.contentSize;
CGSize webViewSize = webView.bounds.size;
CGFloat scaleFactor = webViewSize.width / contentSize.width;
webView.scrollView.minimumZoomScale = scaleFactor;
webView.scrollView.maximumZoomScale = scaleFactor;
webView.scrollView.zoomScale = scaleFactor;
webView.scrollView.scrollEnabled = NO;
}
Thanks!
Try this;
NSString *bodyStyleVertical = #"document.getElementsByTagName('body')[0].style.verticalAlign = 'middle';";
NSString *bodyStyleHorizontal = #"document.getElementsByTagName('body')[0].style.textAlign = 'center';";
[webView stringByEvaluatingJavaScriptFromString:bodyStyleVertical];
[webView stringByEvaluatingJavaScriptFromString:bodyStyleHorizontal];

UIWebView with dynamic height inside a UIScrollView in Objective-C

I am trying to make a UIWebView with "dynamic screen height". This webview is inside a UIScrollView, below other components. See the layout (it uses autolayout):
My idea is to provide just one scroll (from UIScrollView - its working) and make the WebView viewport grow depending the content size. And its not working.
What I did to try to do this:
UIWebView property "scale page to fit " is on;
UIWebView is unpaginated;
UIWebView does not allow user interaction.
In my UIViewController:
- (void) viewDidAppear:(BOOL)animated {
NSString *urlString = #"MY URL GOES HERE";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webViewDescription loadRequest:urlRequest];
_webViewDescription.delegate = self;
_webViewDescription.scrollView.scrollEnabled = NO;
_webViewDescription.scrollView.bounces = NO;
}
My UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
_myScrollView.contentSize = webView.bounds.size;
}
When I run the code, it prints the correct UIScrollView size and UIWebView size. The UIScrollView height got bigger, but the UIWebView maintains the same height from the first loading.
I am testing in a real device.
add a height constraint to your webView and make a IBOutlet of that like
#property(strong, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;
load your webview and in web view delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
get webview content height and set webViewHeightConstraint.constant
like below:-
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *str = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
CGFloat height = str.floatValue;
webViewHeightConstraint.constant = height;
}
hope it may help you.
Try to set webView.frame = frame; in viewDidLayoutSubviews()
For setting dynamic height for UIWebView, you need to set the height in webViewDidFinishLoadView method
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
//For Width
//CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = webview1.frame;
frame.size.height = height;
//frame.size.width = width; //Width
webview.frame = frame;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat widthForIpad = CGRectGetWidth(screenBounds) ;
CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
NSLog(#"The iPad width is - %fl",widthForIpad);
NSLog(#"The iPad height is - %fl",heightForIpad);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
scrollView.contentSize = CGSizeMake(320, height+370); //set your required height
else
scrollView.contentSize = CGSizeMake(320, height+350); //set your required height
}
else
{
if ([[UIScreen mainScreen] bounds].size.height == 1024)
scrollView.contentSize = CGSizeMake(320, height+370); //For iPad
}
}
You can get the content size of webview content using webView.scrollView.contentSize.height.
and than you can use this webView.scrollView.contentSize.height to set the contentSize of the scrollview inside webViewDidFinishLoad method.
Like this in webViewDidFinishLoad method
[objWebView setFrame:CGRectMake(objWebView.frame.origin.x, objWebView.frame.origin.y, objWebView.frame.size.width, webView.scrollView.contentSize.height)];
if(objWebView.frame.origin.y+objWebView.frame.size.height > objScrollView.contentSize.height) {
[objScrollView setContentSize:CGSizeMake(objScrollView.frame.size.width, objWebView.frame.origin.y+objWebView.frame.size.height)];
}

UiWebview in IPhone resize as per the content size

I am having an issue like i am using UIWebView in Iphone and data coming as HTML am storing the html data as astring and passing to the UIWebView am getting it in good but i want to Change the UIWebView Size as per the content size
- (void)viewDidLoad {
web.delegate = self;
NSString * str = [dict objectForKey:#"terms"];
[web loadHTMLString:str baseURL:nil];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
CGRect frame = web.frame;
frame.size.height = 1;
CGSize fittingSize = [web sizeThatFits:CGSizeZero];
frame.size = fittingSize;
web.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
web.frame = CGRectMake(10, 100, web.scrollView.contentSize.width, web.scrollView.contentSize.height);
}
set your web view delegate and frame.
yourwebview.frame=[[UIScreen mainScreen] bounds];
yourwebview.delegate = self;
end then use this delegate method to set height.
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGSize contentSize = aWebView.scrollView.contentSize;
NSLog(#"webView contentSize: %#", NSStringFromCGSize(contentSize));
yourwebview.contentsize = contentSize;
}
You could use the delegate method webViewDidFinishLoad: but sometimes this is called before the HTML is fully rendered and that leads to wrong height values.
To make this work reliably you have to wait until the HTML is fully rendered and then use Javascript to send the correct height to your UIWebView.
Please have a look at this blogpost I wrote some time ago.
Do it with your web view's delegate method :
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
webView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
}

How to get content width of uiwebview?

I load Gif image like this
[webView2 loadData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Apple_jumps" ofType:#"gif"]] MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
and I want to set to center position of uiwebview, but I can't to get width of content. Please help.
You can use document.height or document.width from javascript
// in init
_WebView.delegate = YourClass; // probably self
- (void) webViewDidFinishLoad:(UIWebView *)webview
{
int w = [[_WebView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
_WebView.userInteractionEnabled = w > 200;
}
more info here: https://www.albertopasca.it/whiletrue/objective-c-get-uiwebview-html-page-size/

Resources