How to align UIWebView video (from online DB) to center : Xcode - objective-c

I am displaying a video to my UIWebView from an online Database as:
//-------- Creating webview --------//
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 145, 300,190)];
webview.scalesPageToFit = YES;
webview.backgroundColor = [UIColor redColor];
webview.opaque = NO;
[self.view addSubview:webview];
//-------- Loading Video --------//
NSURL *fileURL = [NSURL URLWithString:
[NSString stringWithFormat:#"http://mysite.org/myProject/sample.php?name=123746"]];
NSURL *nsurl=fileURL;
[webview loadRequest:nsrequest];
It is working great. But the video looks like below:
So, Is there is any way to align the video to center of the web view? Anyone please help. thanks.

3 Options.
If the video will always be that size, make the UIWebView just slightly bigger and center that.
Use [webView stringByEvaluatingJavaScriptFromString:#""] to pass in javascript to adjust the css to center it.
If its a webpage you control, add css to the HTML to center the video div in the window

as I was not able to find any good inbuilt solution using Xcode, I have solved the issue with the help of backend and HTML iframes. I've saved the exact url in a data base and using HTML <iframe>, I loaded the video in the webview with tag to the iframes.

Related

video not playing ios 6

I am trying to play videos from url which my web service returns using MPMoviePlayerViewController. A sample url returned is:
http://view.vzaar.com/1128188/player
Which i pass to MPMoviePlayerViewController. Do you think this url is compatible to be played?
I looked at the source of that page, and it's an html page. I did find a html5 source url. Try:
http://view.vzaar.com/1128188/video
Yes, with above url. I just tried it in xcode and I got it to work.
NSURL *url = [[NSURL alloc] initWithString:#"http://view.vzaar.com/1128188/video"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];
[self.moviePlayer.view setFrame:self.view.bounds]; // player's frame must match parent's
[self.view addSubview: self.moviePlayer.view];
[self.moviePlayer play];
No. It can't be played because it's an HTML page with a flash player embedded. Change your web service to return the video directly, possibly in a different URL.

Mac App WebView background

How is it possible to set the an image as background of the WebView in a Mac App? Because the WebView has that "bouncing" effect, if you scroll further down than the content, the background is usually just white. Is it possible to set a Background-image like it is possible for the UIWebView?
EDIT: The alternate way would be to completely deactivate the bouncing-effect of the webView.
If you are creating this webview programically then create an image then create the webview. Make sure their positions (x,y) and sizes (height, width) are the same.
If you are doing this through IB put down an image (litterally just drag an image) then put the webview on top of it.
Here is a way I found that stops the bouncing:
id sub = self.web.subviews[0];
if ([sub isKindOfClass:[WebFrameView class]])
[sub setAllowsScrolling:NO];
The class check may not be needed, but I like to make sure.
We can add background image in WebView using cocoa. to do this we need to add css for body tag.
Create a webview:
WebView *webView;
Fetch image form bundle path:
NSString *path = [[NSBundle mainBundle] pathForResource:#"my-bg" ofType:#"jpg"];
write Css for body tag.
NSMutableString *htmlBuilder = [[NSMutableString alloc] init];
[htmlBuilder appendString:#"body {"];
[htmlBuilder appendString:[NSString stringWithFormat:#" background-image: url('file://%#');",path]];
[htmlBuilder appendString:#"}"];
Load this image on webview
[[webView mainFrame] loadHTMLString:htmlBuilder baseURL:nil];
[webView setDrawsBackground:NO];
Your image will be added in background on webview.

Youtube embedded in UIWebView causes crash on iPad when entering full screen

I'm trying to embed a Youtube video using a mixture of this technique, and this Youtube SDK blog post in a universal app. The iPhone version, using the same code, works fine.
On the iPad the video does embed, and it plays fine in it's embedded form, but as soon as you tap the full screen button the app crashes (buttons do not respond, the device does not rotate). The music from the Youtube video keeps playing.
There is no error message logged but the app does register as 'Paused' or hung in xCode. Every time it crashes com.apple.libdispatch-manager is on thread 2. Ask me questions and I'll give you more information about the error, but I'm not sure where to start.
I have tried:
changing the size of the UIWebView frame
the UIWebView is in a UIScrollView, but if I take it out of the scrollview and add it to the view the problem is identical.
changing the video
changing the html that I use in the UIWebView from this to this, with no result
changing the format of the youtube link from ?v=uniqueID to /v/uniqueID
checking the presenting view is the rootviewcontroller (it is, but the video is embedded in a modal, which is not the rootviewcontroller).
I am building for iOS 5.1, this doesn't happen if running on iOS6.
The View that the video is embedded in is modal, both on the phone and the iPad. There's no hackery or unusual things happening in the app.
There seems to be talk of Evernote's app having a similar problem, but I don't know if it is related or not.
For your reference, here is the YouTubeView subclass (which subclasses UIWebView):
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// Create webview with requested frame size
self = [[YouTubeView alloc] initWithFrame:frame];
// HTML to embed YouTube video
// NSString *youTubeVideoHTML = #"<html><head>
// <body style=\"margin:0\">
// <embed id=\"yt\" src=\"%#\"
// type=\"application/x-shockwave-flash\"
// width=\"%0.0f\" height=\"%0.0f\">
// </embed>
// </body>
// </html>";
NSString *youTubeVideoHTML = #"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = %0.0f\"/></head><body style=\"background:#FFF;margin-top:0px;margin-left:0px\"><div><object width=\"%0.0f\" height=\"%0.0f\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>";
// Populate HTML with the URL and requested frame size
// NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
NSLog(#"html:\n %#", youTubeVideoHTML);
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, frame.size.width, frame.size.width, frame.size.height, urlString, urlString, frame.size.width, frame.size.height];
NSLog(#"html:\n %#", html);
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
Modal view on iOS 5.0 and iOS 5.1 is the problem that causes crash on full screen video, AFAIK. They just changed hierarchy of views in that version of iOS (parentViewController and presentingViewController) and that is the aftermath. I asked about it long time ago here and one more same question is here and still no one knows what to do.
First of all, they fixed it in 6.0, I guess, that's good.
For 5.1 we changed design a little and avoided modal view. Do it, if it is possible in your situation.

To Convert HTML doc to image in cocoa

is it possible to convert the HTML page to image in cocoa?
Actually i have created the complete view in the HTML and now i want to convert the whole html preview to the image (any jpeg or png etc.).
I couldn't find any resource or sample on the web, which provides some sort of help on my above queries.It's highly appreciated if someone could share his wisdom on how I can achieve this.
Thanks in advance..
First off, I'd like to thank sergio... his answer got me started but I thought I'd share some of the code that I didn't find obvious that I had to write to make it work:
Here's how to make a thumbnail for a page without ever having it displayed:
// Your width and height can be whatever you like, but if you want this to render
// off screen, you need an x and y bigger than the superview's width and height
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(largerScreenDimension, largerScreenDimension, largerScreenDimension, largerScreenDimension)];
[self.view addSubview:webView]; // UIWebViews without an assigned superview don't load ever.
webView.delegate = self; // or whoever you have implement UIWebViewDelegate
webView.scalesToFit = YES; // This zooms the page appropriately to fill the entire thumbnail.
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Then implement this in your delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *webViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *thumbnailData = UIImagePNGRepresentation(webViewImage);
[webView removeFromSuperview];
}
Finally, to display this thumbnail you'll need something like:
thumbnailImageView.image = [UIImage imageWithData:thumbnailData];
As a bonus thing I'll mention, I wanted multiple thumbnails to be generated at once. I found using objc_setAssociatedObject() and objc_getAssociatedObject() to be very helpful with keeping track of which webView was loading which thumbnail. Going into detail on how that worked is beyond the scope of this question, though.
You can draw your view in an image context, like this:
UIWebView* view = ...
....
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imagedata = UIImagePNGRepresentation(viewimage);
NSString *encodedString = [imageData base64Encoding];
Another option would be using Quartz PDF engine to create a PDF.

How to play YouTube Movie on an iPhone Application when tapped on UITableViewCell?

I'm a newbie to iPhone development and just started on developing an application that contains a UITableView where each cell consisting of youtube video thumbnails in the form of webviews. For embedding YouTube Player on iPhone, I have used the follwing piece of code.
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
Now that I can see the thumbnail on the tableviewcell and once I tap on the thumbnail, YouTube player opens and play the movie.
My thumbnail occupies only a small portion of the cell and the rest of the area of the cell contains some text descriptions. My problem is that I must tap exactly on the thumbnail for the movie to play. If I tap somewhere else on the cell then it wouldn't play because my thumbnail doesn't extend all over the cell.
Isn't there a way to make the movie to play in didSelectRowAtIndexPath? I have seen some chaps suggest using Javascript but nobody seem to have an idea on the correct way of using it for this problem.
Highly appreciate it if anybody can help.
I suggest you to take a look at YouTube APIs and try to figure it out what is your real problem. but another good link is How To Play YouTube Videos Within an Application. Hope I'm helping you.
It's maybe not the best solution but It works:
//get your UIWebview in tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//...
videoView.mediaPlaybackRequiresUserAction = NO;
[videoView loadHTMLString:[self generateHtmlEmbedYouTube] baseURL:nil];
From the Documentation
mediaPlaybackRequiresUserAction:
A Boolean value that determines
whether HTML5 videos can play
automatically or require the user to
start playing them.
The onely problem with this solution is that you have to reload your UIWebView.