NSData from Shorted URL not make UIImage - ios

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

Related

Display image on UIImageView using Web server image

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];

Getting nil image even though the file exists in directory

I am saving an image from to my apps directory, and when I try to retrieve it with imageWithContentsOfFile it returns a nil image. I have verified that the file exists in the apps container and that the path I use is correct. Any ideas?
If there is any code that you need let me know. I am not sure what you would need to see.
Thanks
BTW here is the exact syntax I use for loading the image from the path.
UIImage *image = [UIImage imageWithContentsOfFile:path];
The first thing you should do is to check is it a file input/output issue or an image issue. So try to read your file data with
NSData *fileData = [NSData dataWithContentsOfFile:path];
If fileData is nil or its length is 0 then you should recheck the path and the way you are storing a data.
If fileData is ok try
image = [[UIImage alloc] initWithData:fileData];
to check a data to image conversion. Is it possible to open file with some MacOS image viewer?

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

Displaying base64 encoded image in rails

Update:
I took the base64 string iOS was generating and decoded it to a binary file through a website. The image was encoded properly it seems.
My problem then is with the rails side of things. I'm displaying the image using the following code but it's just showing a broken image.
<td><%= ('<img src="data:image/jpg;base64,%s">' % Base64.encode64(user.imagestring)).html_safe %></td>
I'm trying to encode a base64 string to send through JSON to my web service (Rails using Postgresql). I've converted a UIImage to NSData and am now converting it to a base64 string.
I'm using the new base64EncodedStringWithOptions method. It converts to a long string but on my web service the image appears broken.
I uploaded the image to an image to base64 converter website and it returns a slightly different string.
Is the problem with the iOS encoder or am I doing something wrong?
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *blob = [imageData base64EncodedStringWithOptions:0];
You are doing a round-trip through UIImage and UIImagePNGRepresentation(image) to get the data to be base 64 coded. That might not be the same contents as the original file. There are tons of options when coding images, and you're not assured that your roundtrip will use the same options.
I would, instead, suggest just loading the NSData directly:
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:path];
Try running that through your base 64 coding and see what you get.
The problem was with the ruby code. This worked:
<td><%= ('<img src="data:image/png;base64,%s">' % user.image).html_safe %></td>

Resources