http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1
I'm trying to download image on imageView. If you will paste this url on browser it will show, but on imageView it's not showing. If you will try any other image, then the same code will work, but when I used this url, the image does not show.
The image in question seems to be a WebP image (served with the wrong MIME type of image/png), which is not a format natively supported by UIImage. However, you can use iOS-WebP to decode the image:
Add the framework, header and implementation to your project, then use:
#import "UIImage+WebP.h"
NSURL *url = [NSURL URLWithString:#"http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1"];
NSData *data = [NSData dataWithContentsOfURL:url];
imageView.image = [UIImage imageWithWebPData:data];
And please remember to do the download and decoding steps asynchronously so as not to block the main UI.
Try this?
NSURL *url = [NSURL URLWithString:#"http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.img.image = [UIImage imageWithData:data];
Edit
The URL you provided is not an absolute path hence the data being fetched cannot be converted into an UIImage. There is something wrong with the URL or the formatting of it.
Related
I'm writing an image to the simulators cache directory using
NSURL *cacheDir = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask].lastObject;
NSURL *avatarFileURL = [cacheDir URLByAppendingPathComponent:#"test.png" isDirectory:NO];
NSData *imageData = UIImagePNGRepresentation(avatarImage);
[imageData writeToURL:avatarFileURL atomically:YES];
This works fine and I've verified the image by logging the avatarFile url and loading it into a browser, which displays the cache image.
I then try and read the test image back and I'm getting different results if I use imageWithContentsOfFile: and imageWithData: as follows:
NSURL *cacheDir = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask].lastObject;
NSURL *avatarFileURL = [cacheDir URLByAppendingPathComponent:#"test.png" isDirectory:NO];
// Returns a nil.
NSString *avatarFile = avatarFileUrl.absoluteString;
UIImage *image1 = [UIImage imageWithContentsOfFile:avatarFile];
// Returns the image.
NSData *imageData = [NSData dataWithContentsOfURL:avatarFileUrl];
UIImage *image2 = [UIImage imageWithData:imageData];
I'm at a loss to explain why imageWithContentsOfFile fails and imageWithData works when they are both addressing the same URL. I've verified the URL by logging it and urls are exactly the same.
Anyone know why imageWithContentsOfFile is not working?
Your issue is with this line:
NSString *avatarFile = avatarFileUrl.absoluteString;
Look at the value of avatarFile. It will be something like file:///..... imageWithContentsOfFile: expects a proper file path, not a file URL.
You need to properly convert the file URL into a path. That is done as follows:
NSString *avatarFile = avatarFileUrl.path;
I'm trying to display a BLOB image (get from web server using Json) in my iOS app, but when I run my application I get an empty UIimageView, here is my code :
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:encodedUrl]];
NSData *profileImage1 = [[NSData alloc] initWithBytes:[dataURL bytes] length:[dataURL length]];
UIImage *profileImage2 = [UIImage imageWithData:profileImage1];
[profilImage setImage:profileImage2];
How can I fix this problem?
[UIImage imageWithData:data] only parses known image file formats like JPEG, PNG, etc. (Full info in the decomentation). Passing blobs isn't supported by UIImage. You need to do some decoding to be able to use the data for the UIImage. You can use GMTBase.64 for encoding and decoding of data. Read the docs and other posts and you'll find out how to change your code.
Hope this helps.
In my project i need to show the different sizes of images in zig-zag fashion. so, i converted the uiimages(url) which are coming from service to NSData and then i get the uiimage. my code is
NSURL *url = [NSURL URLWithString:[[_result objectAtIndex:i ] valueForKey:#"PImage"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
so i can get the image size(width and height), But my problem is according to the image size, i need to create UIView, this code is works fine for me, but it is taking too much of time(almost 25 sec) to load 8 images. i figured converting UIImage to NSData is taking time. Is there any way to get the image size(width and height) without converting it into NSData
Thanks for spending time for me.
You can get image properties without actually loading whole image data from disk using ImageIO framework:
#import ImageIO;
...
NSURL *imageURL = … // Init URL somehow
CGImageSourceRef imgSource = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
NSDictionary* imageProps = (__bridge_transfer NSDictionary*) CGImageSourceCopyPropertiesAtIndex(imgSource, 0, NULL);
NSLog(#"%#", imageProps);
CFRelease(imgSource);
Image width and height will be stored in dictionary under PixelHeight and PixelWidth keys (tested with png image, may be other image formats will use different keys)
Instead of converting url to data and to UIImage, Use EGOImageView OR AsyncImageView. You can simply pass the URL to them. Again setFrame based on size of the image.
Im trying to attach a Google Maps image to an email generated in my app, however Google Maps does not return a specific image from its url. If I search a url with .png etc at the end of it, I can get the image fine, but how can I get the image from a site that doesn't have that,
ie:
http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap\\&markers=size:mid%%7Ccolor:red%%7CNew+York&sensor=false
Im using NSData dataWithContentsOfURL: which then attaches to my email.
I have also tried to open the url in an iframe within the email with the same result, works with a .png url, not maps.
Any help would be greatly appreciated, thanks.
Try this,
NSString *urlString = #"http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap\\&markers=size:mid%%7Ccolor:red%%7CNew+York&sensor=false";
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSData *myData = [NSData dataWithContentsOfURL:webURL];
UIImage *image = [[UIImage alloc] initWithData:myData];
self.imageView.image = image;
http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap%%5C&markers=size:mid%%257Ccolor:red%%7CNew+York&sensor=false
Just needed to edit some of the % \ etc to incorporate the UTF8 encoding, however if you use the encoding method karthika suggested, it breaks the search. All I did was hard code the string and removed the added encoding before the address and its working perfectly.
I'm new to using xcode, so any information or links would be helpful. If I had a RANDOM website url in coredata, how would I be able to direct my UIImage to get the photo from that website to display in iOS?
Let's say imageURL is a NSString * ivar or property where you hold your URL.
To get the image this should work:
NSURL *url = [NSURL URLWithString:imageURL];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *myImage = [UIImage imageWithData: imageData];
Of course you should add some error checking but this should get you into the right direction.