Display image on UIImageView using Web server image - ios

I want to display an image on UIImageView calling the Web server image.
Because I want to replace and show it right away.
here's the code
NSURL *imageURL = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
webImage.image = [UIImage imageWithData:imageData];
The problem is...
The image is displayed well at first,
but if I replaced the image on the web server with another image.(The file name is the same, only the file is replaced) It didn't change and still displayed the first image.
Please help me...

The code in the question seems flawed, i.e. you apparently do a network request in the UI thread. Such approach tends to make your application UI unresponsive. However, if we boil down the question to "Why NSData returns outdated data for the same URL?" then the answer is because it has internal caching. The caching logic could controlled either by the the response headers from the server (e.g. Expires) or with NSDataReadingUncached option (to suppress any caching logic):
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];

Related

Need to process a request to youtube inside an iOS app

If I take a browser and type http://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en into it, I get a bunch of text describing video with ID $id in a form of a file, that gets downloaded by a browser. The file contains a large string that is unique every time the request is performed.
Now, I need to gain access to that giant string inside iOS app.
Could you please tell me where to start digging? UIWebViews? Or maybe there's a simple solution?
Thanks in advance.
NSURL *url = [NSURL URLWithString:#"https://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *idString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Problems on downloading html page through NSData

I am currently trying to download the html code of a page for an app I am working on. For some reason when I try to download the code through
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://www.fedex.com/fedextrack/?tracknumbers=%#,#"Tracking#"]]];
I dont get the right HTML code however. When I set up the same load request through a UIWebView have it load then then get the HTML code from stringByEvauluatingJavaScript:#"document.body.innerhtml" I do get the right one.
My question is, how do I make the dataWithContentsOfURL work and get the right code?

How can I open an image from file system into UIImage View

Before anyone down votes my question, I have literally looked all over stack and cannot find the answer
I am making a phonegap app which I can place an image into my filesystem
the url :
file:///Users/danielnasello/Library/Application Support/iPhone Simulator/7.0.3/Applications/75EE3563-560D-4CFD-B357-313DD559573D/Documents/Vault/1386252707450.jpg
I can then pass this url to my modal controller to present an image in the image view.
the problem is I cannot seem to find the correct way to access the image from my filesystem and display it into my image view.
my current relevant code
(self.myImage) is the string i pass from phonegap. it does contain the url string because I am logging it, so I know the url is getting passed. However, the image simply will not display.
I tried using image named from one of my library images and it works fine. I just cant seem to find the correct way to present it using file url.
NSURL *url = [NSURL URLWithString:self.myImage];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
self.imageView.image= img;
here is the code for that.
NSString *filepath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"1386252707450.jpg"];
self.imageView.image=[UIImage imageWithContentsOfFile:filepath];
this code is actually correct. It just doesn't work on simulators (shocker), you need a real device

NSData from Shorted URL not make UIImage

I am facing problem with downloading a .jpg image using a shorted URL like this one:
shorted URL:- https://db.tt/KH5NgfT1
I got successfully NSData length of 41791 bytes. But problem is when I convert this NSData to UIImage it gives NULL and when this NULL image I am posting on faceBook It successfully posted to my facebook account, for post on facebook I am using SLComposeViewController and one thing more that is this Image is also not shown in SLComposeViewController.
I am using this code for download image
NSData *downloadedImageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:shortedURL]];
Convert to UIImage
UIImage *image=[UIImage imageWithData:downloadedImageData];
returns NULL image
and My SLComposeViewController look like this
My SLComposeViewController screenshot
I want to show downloaded image in SLComposeViewController
That shortened URL is not redirecting you to an image file as it is hotlink protected. It redirects you to a simple HTML page where you can see the image. So, NSData you get using dataWithContentsOfURL is not an image file data, and imageWithData method returns nil as expected.
And you say it is displayed correctly on Facebook when you post it, then it looks like Facebook handles this Dropbox image hotlink protection itself to get the image directly.
You might need to do the same trick by yourself.
For example, direct hotlink to your image is: https://photos-3.dropbox.com/t/0/AAAZy24NIIDzdh-J7L2fei34nM1AMnuBbLcV-nc2VAvpTg/12/105579065/jpeg/1024x768/3/1383656400/0/2/04-11-2013_16-53-16.jpg/ooh2MQDiN0DOjsEiwM68rI2buAyfTtbfog-UzrmvMqw
NO erkanyildiz maybe your above answer is wrong because there is no difference between long and shorted URL they both redirect me to a image once look at this
this shorted url not works because it's from DropBox
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://db.tt/KH5NgfT1"] options:NSDataReadingUncached error:&error];
UIImage *downloadedImage=[UIImage imageWithData:imageData];
this shorted url works because it's NOT from DropBox
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://goo.gl/FTh7No"] options:NSDataReadingUncached error:&error];
UIImage *downloadedImage=[UIImage imageWithData:imageData];
this long url not works because it's from DropBox
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://www.dropbox.com/s/5443wq99wu9a0bu/IMG_3679.PNG"] options:NSDataReadingUncached error:&error];
UIImage *downloadedImage=[UIImage imageWithData:imageData];
this long url works because it's NOT from DropBox
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.wired.com/wiredenterprise/wp-content/uploads/2013/07/20130627-DROPBOX-OFFICE-147edit.jpg"] options:NSDataReadingUncached error:&error];
UIImage *downloadedImage=[UIImage imageWithData:imageData];
So, finally I think the problem is at DropBox server may be they have some kind of web-encription so that we can't download image directly using NSData dataWithContentsOfURL:
and may be because of web-encription it support to Facebook.
solution is DropBox provides api for downloading image
https://api-content.dropbox.com/1/files//
DropBox Core API Documentation
Thanks for Replying

UIWebView loadHtmlString not working on device

I have a webview which i want to load using the loadHtmlString method. The problem is that I want to be able to change the img src's with images that i have previously downloaded. I also use google analitics in the html so I need to set the baseUrl to the actual url so it will work. Here comes the problem. If I put the baseUrl in, the images will not load. If I don't set the baseUrl, it works. How can I get around this, so I will be able to both use google analitycs and have the images store locally in my application? I would prefer not having to implement the google analitics sdk in my project.
A strange thing is that if I run it in simulator, and not put the "http://" prefix in front of my baseUrl, it works fine. However, when I run it on a device, I receive the following error and it doesn't work:
Domain=WebKitErrorDomain Code=101 "The URL can’t be shown"
Thanks
EDIT
If I do this, it works:
[appsWebView loadHTMLString:htmlString baseURL:nil];
However, I must provide a baseURL in order to have Google Analitics working, I have two further cases:
This one gives the above mentioned error: (it works ok in simulator but gives error when running on device)
[appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:#"test.com"]];
This one simply doesn't show anything: (neither loads the html string or the url)
[appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:#"http://test.com"]];
I incorrectly assumed that the problem was that the local image was not fully specifying the full path, but that does not appear to be the problem here. But, you are quite right that it appears (somewhat surprisingly) that you cannot specify some web-based baseURL and also reference a local image in your HTML string. No simple solutions are leaping out at me, but at the very least, it appears that you might have a couple of (not very good) options:
First, you could base64 encode the local image using some base64 library like Mike Gallagher's NSData+Base64 category, e.g.:
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSString *imageDataBase64 = [imageData base64EncodedString];
NSString *imageHtml = [NSString stringWithFormat:#"<img src='data:image/png;base64,%#'>", imageDataBase64];
This slows the initial rendering, but maybe it's better than nothing.
Second, you could always try leaving the baseURL as nil, removing the JavaScript that does the Google Analytics from the HTML string, and then try injecting that JavaScript via stringByEvaluatingJavaScriptFromString. This approach may or may not work depending upon the complexity of the Google Analytics JavaScript (e.g. what further web-based references it might have), but there's a outside chance you might be able to do something that way.
My apologies for assuming the problem was a trivial img URL. Clearly you had identified a more fundamental issue.
Original answer:
Create your image URLs in your HTML string to be fully qualified file URLs within your local file system:
The image is either in Documents:
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:imageName];
Or in the bundle:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName
ofType:nil];
But, once you have fully qualified path, you should be able to use that:
NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
NSString *imageHtml = [NSString stringWithFormat:#"<img src='%#'>", imageUrl];
I would bet it's a casing issue. Take into account that the Device is case sensitive whereas the Simulator is not. Check the URL and make sure it contains the right characters.

Resources