简体   繁体   中英

Downloading image from server without file extension (NSData to UIImage)

From my server I'm pulling down a url that is supposed to simply be a profile image. The relevant code for pulling down the image from the urls is this:

NSString *urlString = [NSString stringWithFormat:@"%@%@",kBaseURL,profile_image_url];
profilePic = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];

My url is in the format (note no file extension on the end since its dynamically rendered)

localhost:8000/people/1/profile_image

If I load the url in my browser the image displays; however the code above for pulling down the UIImage does not work. I've verified that the code does pull an image from a random site on the interwebs.

Any thoughts on why this is happening?

Does it work if you feed NSURL

http://localhost:8000/people/1/profile_image

I imagine that the http:// is important here (so that the NSURL knows whether it's pointing at a file or remote URL).

The reason you can't display it in a UIImageView is because the UIImageView doesn't know what format it's in (jpeg,png,etc.)
A good way for future coding is always use extensions. Don't take short cuts, they will always end up worse and you'll have to fix them over and over again.

First check for the following :

Permissions: change from read only to read and write.

I have tried many methods in my project to display pic from server and the most prominent issue i have found is that by default, your localhost folders are read only and you need to change permission as well.... Initially i thought read only wudnt matter if i am just loading the picture beacuse technically i am only reading the contents , right. But it did matter.

White Spaces

One more thing you coud check for is the name of your image containing white spaces, if they are then you need to remove white spaces from your image name while making URL request.

Try adding extensions if nothing works out If eveythings fine , try to load the image with extensions which would obviously work.

Provide us with some initial coding as well

Let us know about your code snippets and console outputs which could help the community to reach the solution of the problem in a more prominent manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM