How do i insert UIImage on UIWebView - iphone

I'm trying to display an Animated-GIF image in a UIImageView. Because this way only the first frame is shown i wanted to try a different approach.
UIWebView.
How can i get an Animated-GIF that is in memory (NSData) to display on a UIWebView with all frames..?

Thanks Thomas for you response, i don't know if it works because i managed to do it myself now..
Been decoding the GIFs for more then a week now and still with no succes so i went from UIImageView to UIWebView and the following code made it work..
[self.webView loadData:gifData MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];

I've not tried it, but something like this :
NSString *html = [NSString stringWithFormat:#"<img src='data:image/gif;base64,%#' />", [myData base64Value];
[myWebView loadHTMLString:html baseURL:nil]
You'll need a NSData category that implements converting to base64, that shouldn't be difficult to find (I've bookmarked http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html)
edit: another way is to decode the gif, so you have an NSArray of the frames : http://pliep.nl/blog/2009/04/iphone_developer_decoding_an_animated_gif_image_in_objc
http://blog.stijnspijker.nl/2009/07/animated-and-transparent-gifs-for-iphone-made-easy/

Related

How to show TXT or ASCII file into UIWebView correctly

I'm trying to show TXT file (ASCII) into UIVewView. For example, using the site www.partisani.net/35.txt in Safari on MacBook works fine, Safari on iPhone doesn't. Safari's iPhone shows the file with another layout. Could someone help me, please?
As far as I can tell, the layout of the file on the Mac vs iOS is exactly the same. Are you talking about text wrapping? You can see that on the Mac by resizing the browser.
If you want to handle the line length differently you'll need to do so by setting up scrolling.
UPDATE with more detail:
This "sort of" changes the original content :). Basically, you need to tweak both the contentSize of the webview and embed the text file in some boilerplate HTML to reflow the text rather than have the default viewport width assigned to the text document. The latter I accomplish with something like:
NSURL* url = [NSURL URLWithString:#"http://www.partisani.net/35.txt"];
UIWebView* vw = (UIWebView*)self.view;
vw.delegate = self;
NSData* Data = [NSData dataWithContentsOfURL:url];
NSString* aStr = [[NSString alloc] initWithData:Data encoding:NSASCIIStringEncoding];
NSString* responseStr = [NSString stringWithFormat:
#"<HTML>"
"<head>"
"<title>Text View</title>"
"</head>"
"<BODY>"
"<pre>"
"%#"
"/pre>"
"</BODY>"
"</HTML>",
aStr];
[vw loadHTMLString:responseStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[aStr release];
The vw.delegate = self is important, as you also need to have your controller conform to the UIWebViewDelegate protocol and implement the webViewDidFinishLoad: method. There you can set the scroll width and height of your webview as needed:
- (void) webViewDidFinishLoad:(UIWebView *)webview {
UIScrollView* sview = (UIScrollView*)[[webview subviews] objectAtIndex:0];
sview.contentSize = CGSizeMake(1000, 800);
}
This is an extremely barebones implementation--presumably you would also want logic to calculate the necessary width and height based upon the loaded text rather than use constants as shown here; you'll need some parsing logic associated with the original data for that, but this should get you started.
Setting the scalesPageToFit property to yes might fix your problem.
I'm not sure this is actually something to do with TXT or ASCII, but rather the UIWebview resizing the content.
The file you mentioned loaded into a landscape ipad screen has exactly the same layout as on Mac Safari:
You can change whether or not the UIWebView scales its content with the scalesPageToFit property.

Image Swiping In Objective-C

I'm just a beginner to iphone development.
Recently i'm doing a project in which images need to be swiped. Could any one help me with it?
The images are stored in a server whose link is given.
I'm need it badly. So please help me
thank you
There are two ways.
You could either look at using a Paging Scroll View
Or, You could look at getting a series of UIImageViews with the correct images in each and then setting up a UISwipeGestureRecogniser for both directions. On the swipe's event handler you can adjust the x and y positions of the imageViews.
This is basic stuff. You should read some of the How-to's on Apple's developer website to help you with the base knowledge.
Edit:
Regarding the Internet images, the code can be adapted into the paging scroll view example:
NSString *path = #"http://merrimusings.mu.nu/archives/images/groundhog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
Load the strings from an NSMutableArray instead and you're good to go!
Use Three20 photo Album. Your problem will be solved.

How to reload already loaded uiwebview with new HTML string?

I have implemented UIWebView in my project to show some HTML data. The problem is I have loaded the webview with some HTML string by using the method
[myWebView loadHTMLString:Data baseURL:nil];
Now I want to add action of button such that when user click on the button the new data is loaded in the webview. such that
-(void)clicked{
[myWebView loadHTMLString:newData baseURL:nil];
}
I tried by applying the same as above but no affect was there. I also tried [myWebView reload], but nothing happened.
Plz tell me the appropriate method.
TRy with, First call stopLoading then reload on your UIWebView instance.
[myWebView stopLoading ];
[myWebView reload];
loadHTMLString:baseURL: is the correct method.
But still some things might be wrong, such as the data you are trying to send to the webview. Did you try logging your newData object to see what is being send to the webView object?
Is your newData string loaded from a resource file? If so, you could try loading you html data from the resources with something like the code bellow
NSString *html = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"myFileName" ofType:#"html"]];
[myWebView loadHTMLString:html baseURL:nil];
Even after trying accepted answer I have not got my issue solved.
So, after spending some time I found below logic which works for me:
Objective-c
dispatch_async(dispatch_get_main_queue(), {
[myWebView reload];
})
Swift
dispatch_async(dispatch_get_main_queue(), {
myWebView.reload()
})

How to get path to add number of Images that are in the resource to be displayed in the WebView in objective-c

I am added image to my application but I want to display them into my web view. I tried but not succeeded. how can I set the path for those images in html in image tag and how to display them.
Please help me out of this.
Thank you,
Madan Mohan.
Are you just displaying the images individually or do you have an HTML file that references them?
To get a URL to the files directly:
NSString *pathForImage = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
NSURL *URL = [NSURL fileURLWithPath:pathForImage];
If you're loading an HTML file that references the images (such as by including an <img src="image.png">, with no path info) then:
[webView loadHTMLString:htmlString
baseURL:[[NSBundle mainBundle] resourceURL]];
EDIT: so, supposing you had image.png in your application bundle as a resource, then you could do:
[webView loadHTMLString:#"<html><body><img src=\"image.png\"></body></html>"
baseURL:[[NSBundle mainBundle] resourceURL]];
In your HTML for the webview do
<img src="imagename">
Where imagename is the filename of the image. The iPhone doesn't have any folder structures or anything like that. That means that even if you have groups or folders set up in XCode, at runtime they are all lumped into one, giant directory.
If that doesn't work right away, look at what your are setting your base url to be. It might be that you need to set the baseURL to nil to remind the phone to look locally.
The following code sample might fix the problem for you.. Good luck!
NSData * image = [[NSMutableData alloc] initWithContentsOfFile:#"image.png"];
[self.webView loadData:image MIMEType:#"image/png" textEncodingName:nil baseURL:nil];
[image release];
[self.view addSubView:webView];

iPhone: How to Display Text from UIWebView HTML Document in a UITextView

I have an RSS feed that gets arranged in a UITableView which lets the user select a story that loads in a UIWebView. However, I'd like to stop using the UIWebView and just use a UITextView or UILabel.
This png is what I am trying to do (just display the various text aspects of a news story):
I have tried using:
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];
and assigning the string to a UILabel but it doesn't work from where I am implementing it in webViewDidFinishLoad (--is that not the proper place?). I get a blank textView and normal webView.
If I overlay a UITextView on top of a UIWebView on its own (that is, a webView that just loads one page), the code posted above works displays the text fine. The problem arises when I try to process the RSS feed .
I've been stuck wondering why this doesn't work as it should for a few days now. If you have a better, more efficient way of doing it then placing the code in webViewDidFinishLoad, please let me know! Does it go in my didSelectRowAtIndexPath?
Thank you very much in advance!
I think the you should first log the string returned by :
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];
... in the case of the RSS feed to make sure that you are getting something back. It's possible the RSS page doesn't have the same javascript components and that it returns an empty string.
Once you've confirmed that, then it becomes a simple matter of getting it to display properly in the text view.
If the NSString you want to display is not empty, try to do something like this in the webViewDidFinishLoad method:
[yourUILabel performSelectorOnMainThread:#selector(setText:) withObject:[NSString stringWithFormat:#"bla bla %#", #"more bla"] waitUntilDone:YES];
The main thread of an iphone app is responsible for drawing components, that is why your label doesn't show your text.
You could also try setting setNeedsDisplay: to true
Also, the UILabel will not preserve the HTML format. It will display it as just text.
You could try the following:
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.innerHTML;"];
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML;"];
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerText;"];
You lose out on formatting information with the last line of code.