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.
}
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];
}
I am trying to load a html file in a web view and the screen blacks out.
Hierachy of view controller are:
View
Web View
Navigation Item
This is the tab and there is navigation controller to present the View Controller.
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView = [[UIWebView alloc] init];
self.view.backgroundColor = [UIColor colorWithPatternImage:[JLTBackgroundHelper getBackgroundImageForLandingView]];
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor clearColor];
UIViewController *rootVC = [[UIViewController alloc] init];
rootVC.title = #"Contact JLT Sport";
rootVC.view = self.webView;
self.viewControllers = #[rootVC];
NSString *fileName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad-specific
fileName = #"contact_ipad";
} else {
// iPhone-specific
fileName = #"contact";
}
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:fileName ofType:#"html" inDirectory:#"Assets/web"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate= self;
}
- (BOOL) webView:(UIWebView*)wv shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navType
{
if (navType == UIWebViewNavigationTypeLinkClicked
&& [request.URL.scheme hasPrefix:#"http"])
{
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
Not sure whats going wrong. tried for more than 5 hours but no luck.
Instead of
self.viewControllers = #[rootVC];
Try this
[self presentViewController:rootVC animated:NO completion:nil];
If presenting the controller in viewDidLoad gives some problem, then do it in viewDidAppear. First of all I'm not clear why you are creating another view controller within viewDidLoad of a controller and adding the subview to that.
I have a NotificationVC containing table view having 10 cells index 0 to 9. on click of each cell NotificationWebVC should open up that has a UIWebView that loads diff. html depending upon which cell is selected in NotificationVC. Right now my code works only for index=0 , but not for other indexes , seems bit weird.
NotificationWebVC
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:#"html" inDirectory:nil] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
Please suggest. _resourceName is a property having the name of html file.
try this code
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:#"html" inDirectory:nil] ;
[_webView loadHTMLString:htmlFile baseURL:nil];
_webView.delegate=(id)self;
First you need to make an array containing your 10 urls. Then when a user tap on each cell, use the didSelectRowAtIndexPath delegate method.
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:[resources objectAtIndex:indexPath.row] ofType:#"html" inDirectory:nil] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
Add line of code to NotificationWebVC.h
#property (nonatomic,retain)NSString *resopnseString;
then in .m class synthise it
#synthesize resopnseString;
after this go to didSelectMethod of tablevew datasouce in NotificationVC.m
there you add the following code
NotificationWebVC *notif=[[NotificationWebVC alloc]init];
notif.resopnseString=[resources objectAtIndex:indexPath.row];
//here it may present view or push view or what ever it be
After doing all this now you shift to NotificationWebVC.m
add the following code to in viewDidLoad method
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:resopnseString ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
after doing all this pl z be sure to add all 10 HTML file in your project
and try to check that you are getting correct html file on select in table from
be sure you are passing correct name of file in responsString
[resources objectAtIndex:indexPath.row];
or better put
NSLog(#"%#",[resources objectAtIndex:indexPath.row]);
to it check it.
i think this code will help you
// docfileName should be your file name call this method in tableview delegate didSelectrow
-(void)loadLocaldataonWebview :(NSString *)docfileName
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:docfileName ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];
}
then in viewDidload add the following code
-(void)viewDidLoad{
documentsWebView=[[UIWebView alloc]init];
documentsWebView.delegate=self;
documentsWebView.frame=CGRectMake(0, yAxis, kWidth, 616);
documentsWebView.backgroundColor=[UIColor clearColor];
[self.view addSubview:documentsWebView];
}
My code is working fine for iOS 7 but crashes in iOS 6. My app is working fine but the problem occurs when i click on a tab else its working fine.
Checked the link :EXC_BAD_ACCESS While switching tabControllers but no luck.
Below is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView = [[UIWebView alloc] init];
self.view.backgroundColor = [UIColor colorWithPatternImage:[JLTBackgroundHelper getBackgroundImageForView]];
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor clearColor];
UIViewController *rootVC = [[UIViewController alloc] init];
rootVC.title = #"Contact JLT Sport";
rootVC.view = self.webView;
self.viewControllers = #[rootVC];
NSString *fileName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad-specific
fileName = #"contact_ipad";
} else {
// iPhone-specific
fileName = #"contact";
}
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:fileName ofType:#"html" inDirectory:#"Assets/web"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate= self;
}
//send external URL requests to Safari
- (BOOL) webView:(UIWebView*)wv shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navType
{
if (navType == UIWebViewNavigationTypeLinkClicked
&& [request.URL.scheme hasPrefix:#"http"])
{
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
#end
Plese guide/suggest.
try this :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView = [[UIWebView alloc] init];
self.view.backgroundColor = [UIColor colorWithPatternImage:[JLTBackgroundHelper getBackgroundImageForView]];
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor clearColor];
UIViewController *rootVC = [[UIViewController alloc] init];
rootVC.title = #"Contact JLT Sport";
rootVC.view = self.webView;
self.viewControllers = #[rootVC];
NSString *fileName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad-specific
fileName = #"contact_ipad";
} else {
// iPhone-specific
fileName = #"contact";
}
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:fileName ofType:#"html" inDirectory:#"Assets/web"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.delegate= self;
}
if above solution does not work then try this:
Debug with Instrument
Go to Product >> Profile >> All > Zoombies
Press "Record" button on top left corner
Perform your task and when application get EXC_BAD_ACCESS the instrument give that line where the application was being crash.
Also debug in this method to:[JLTBackgroundHelper getBackgroundImageForView];
**Make sure that all variable is being get memory before use it.**
May be the resin is that some method is depreciated in iOS 7.
These are the possible scenario of application is being crash and its solution.
UPDATE #1: forgot to mention this is running on an iPad app
This is my revised code (still not working, but got rid of the unnecessary code):
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:#"custImage"] URLByAppendingPathExtension:#"png"];
NSError*writeError = nil;
[client.aClientImage writeToURL:fileURL options:0 error:&writeError];
NSAssert(writeError==nil, writeError);
// write appointment info
NSString *htmlString;
if(client.aClientEMail.length > 0) {
htmlString = [NSString stringWithFormat:NSLocalizedString(#"HTML_STRING1",nil),
client.aClientFirstName,
client.aClientLastName,
client.aClientEMail.length == 0? #"": client.aClientEMail,
client.aClientPrimaryPhone,
apptSelected.aServices,
fileURL];
}
else {
htmlString = [NSString stringWithFormat:NSLocalizedString(#"HTML_STRING2",nil),
client.aClientFirstName,
client.aClientLastName,
client.aClientPrimaryPhone,
apptSelected.aServices,
fileURL];
}
When I look at custImage in the XCode debugger, I see a different image from the previous image, which is correct. However, when it gets time to display the image at fileURL, it's a totally different image than custImage and is the same image that was displayed the first time!
UPDATE #2: I have figured out that fileURL has the correct image, but it is NOT being written to the device the second time (the first image is not being replaced).
UPDATE #3: this is the contents of htmlString that is displayed in the UIWebView popover:
<html><head><style type="text/css"> body {font-family: "Verdana"; font-size: 12;} </style></head><body>
<h2>Rolf Marsh</h2><p>phone: 213-555-1234<p>services: Art, Decals<p><img src="file:///private/var/mobile/Applications/FEE7159E-1FF8-4B94-A446-2A4C72E0AD41/tmp/custImage.png"/></body></html>
Any suggestions on how to fix this?
you never write the data to disk as far as I can see.
Only in the commented out section you attempt to.
Write it in between:
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:#"custImage"] URLByAppendingPathExtension:#"png"];
//write
NSError*writeError = nil;
[client.aClientImage writeToURL:fileURL options:0 error:&writeError];
NSAssert(writeError==nil, writeError);
//read / use in the html or so
...
Dont forget to reload the web view or whatever you use to show the html
I figured it out... for others that are having the same problem, here is the code that works. Notice that I do NOT save it to a temporary file, since I already have the image in the Core Data entity. Here's the code:
-(void) showExistingAppointmentDetails: (AppointmentInfo *) apptSelected {
// make rectangle to attach popover
CGRect rectangle = CGRectMake( [apptSelected.aPosX floatValue], [apptSelected.aPosY floatValue],
[apptSelected.aPosW floatValue], [apptSelected.aPosH floatValue]);
// see if this is for staff; if so, don't display anything
if([apptSelected.aApptKey isEqual: #"Staff"])
return;
NSPredicate *predicate = ([NSPredicate predicateWithFormat: #"aClientKey == %#", apptSelected.aApptKey ]); // was aApptKey
ClientInfo *client = [ClientInfo MR_findFirstWithPredicate:predicate inContext:localContext];
UIImage *image = [UIImage imageWithData:client.aClientImage]; // image is good <---------
// write appointment info into html string
NSString *htmlString;
if(client.aClientEMail.length > 0) {
htmlString = [NSString stringWithFormat:NSLocalizedString(#"HTML_STRING1",nil),
client.aClientFirstName,
client.aClientLastName,
client.aClientEMail.length == 0? #"": client.aClientEMail,
client.aClientPrimaryPhone,
apptSelected.aServices,
[self htmlForPNGImage:image]];
}
else {
htmlString = [NSString stringWithFormat:NSLocalizedString(#"HTML_STRING2",nil),
client.aClientFirstName,
client.aClientLastName,
client.aClientPrimaryPhone,
apptSelected.aServices,
[self htmlForPNGImage:image]];
}
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 300)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0]; // frame color?
popoverContent.view = popoverView;
//resize the popover view shown in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(250, 300);
// add the UIWebView for RichText
UIWebView *webView = [[UIWebView alloc] initWithFrame:popoverView.frame];
webView.backgroundColor = [UIColor whiteColor]; // change background color here
// add the webView to the popover
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:nil]];
[popoverView addSubview:webView];
// if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
}
//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:rectangle inView:self
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (NSString *) htmlForPNGImage:(UIImage *) image {
NSData *imageData = UIImagePNGRepresentation(image);
NSString *imageSource = [NSString stringWithFormat:#"data:image/png;base64,%#",[imageData base64Encoding]];
return [NSString stringWithFormat:#"%#", imageSource];
}