Question

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

Was it helpful?

Solution 2

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

  1. 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];
    
  2. 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];
    
  3. 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];
    
  4. 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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top