I have a Gif in a UIWebView, but when the animation ends, it stops.
How I can do to never stop?
This is my code:
self.video is my UiWebView
my gif is videoGif.gif
- (void)viewDidLoad
{
[super viewDidLoad];
self.video.delegate=self;
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"videoGif" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
[self.video loadData:gif MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
self.video.userInteractionEnabled = NO;
self.video.scalesPageToFit=YES;
self.video.contentMode = UIViewContentModeScaleAspectFit;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
self.video.scalesPageToFit=YES;
self.video.contentMode = UIViewContentModeScaleAspectFit;
}
Thanks!!
You can you use UIView Animations. Here i use UIButton for animation. It may be and alternative way. Here my code:
NSMutableArray *imageArray=[NSMutableArray new];
for(int i=1;i<=16;i++){
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"win_%d.png",i]]];
}
[animatedButton setImage:[UIImage imageNamed:#"win_1.png"] forState:UIControlStateNormal];
[animatedButton.imageView setAnimationImages:[imageArray copy]];
animatedButton.imageView.animationDuration = .50;
[animatedButton.imageView startAnimating];
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.
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];
}
I am attempting to use this method I've constructed to save the layer of a UIWebView as an image. That is working but the SVG I have loaded into the UIView is not getting rendered in the image, only the background of the UIWebView.
+ (BOOL)imageFromSVGinWebView:(UIView *)theView usePNG:(BOOL)usePNG; {
UIGraphicsBeginImageContext(theView.frame.size);
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *layerIMGData = nil;
NSString *pathComponent = nil;
if (usePNG) {
layerIMGData = UIImagePNGRepresentation(layerImage);
pathComponent = #"png";
}else {
layerIMGData = UIImageJPEGRepresentation(layerImage,1.0f);
pathComponent = #"jpg";
}
NSString *appFile = [[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:String(#"img.%#",pathComponent)];
NSError *error = nil;
BOOL returnBOOL = [layerIMGData writeToFile:appFile options:NSDataWritingAtomic error:&error];
if(error != nil)
ILogPlus(#"%#",error);
return returnBOOL;
}
In summary, I've loaded an SVG into a UIWebView, saved that UIWebView as an image, opened the image and the image does not have the SVG in it. I have done this with other views successfully, something is different about the SVG or UIWebView.
UPDATE:
If I add a delay to make the app wait a few seconds to execute the image saving code then it works. Putting that code in webViewDidFinishLoad does NOT work. Obviously webViewDidFinishLoad doesn't count SVGs.
I have found that if you add a .1 second delay it will work
- (void)webViewDidFinishLoad:(UIWebView *)webView; {
[self performSelector:#selector(postLoadActions:) withObject:webView afterDelay:0.1f];
}
- (void)postLoadActions:(UIWebView *)webView; {
if (![CCConversion imageFromSVGinWebView:webView usePNG:YES]) {
ILogPlus(#"Didn't work");
}
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[CCFileManagement conversionsDirectory] error:NULL];
ILogPlus(#"%#",array);
//[webView removeFromSuperview];
[webView release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:#"img.png"]]];
imageView.frame = CGRectMake(90, 90, imageView.frame.size.width, imageView.frame.size.height);
imageView.backgroundColor = [UIColor clearColor];
[self.view performSelector:#selector(addSubview:) withObject:imageView afterDelay:2.0f];
[imageView release];
}