Reputation: 135
I tried fetching the image location but its not displaying it in UIImageView, the code which is used for fetching is,
in class.h file,
@property (strong, nonatomic) IBOutlet UIImageView *imageview;
class.m file,
NSString *imagee;
imagee = [result objectForKey:@"image"];
NSLog(@"image is %@ \n",imagee);
the 'imagee' string could return the exact url for the image.
code for image displaying is,
UIImage *imagevieww = [UIImage imageNamed:[result objectForKey:@"image"]];
imageview.image = imagevieww;
but at final i could get the empty space where the image should be placed.
Upvotes: 1
Views: 1411
Reputation: 386
I did the following and it works for me.
NSURL *url = [NSURL URLWithString:@"http://www.yourdomain.com/imagename.png"];
NSData *myData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:myData];
["%storyboardImageName%" setImage:image];
Please note that storyboardImagename is the variable that you put on the xib or storyboard. You will need to change it. Also note the 'setImage' attribute which actually ties it together.
Hope this helps.
Upvotes: 0
Reputation: 135
Found the answer for my problem, thanks for everyone who helped me to find this answer.
UPDATED: Include 'http' with the urlstring so that, it can fetch the correct image from the webservice.
NSString *urlString = [NSString stringWithFormat:@"http://%@",[result objectForKey:@"image"]];
NSLog(@"url image is %@ \n",urlString);
NSData *imageData = [[NSData alloc] init];
imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
NSLog(@"image data is %@",imageData);
imageview.image = [UIImage imageWithData:imageData];
Upvotes: 2
Reputation: 1715
Convert the URL
into UIImage
,
Try This:
NSURL *url = [NSURL URLWithString:@"http://yoursite.com/imageName.png/jpg"];
NSData *myData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[[UIImage alloc] initWithData:myData] autorelease];
Use this image on imageview. If you are getting problem then try to download the image in background. Follow this Link
Upvotes: -1
Reputation: 135
.h
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
.m
Get Image From Webservices:
NSURL *url = [NSURL URLWithString:@"http://www.site.com/image.jpg"];
NSData *myImageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:myImageData];
Set Image to ImageView:
imageView.image = image;
Upvotes: 0
Reputation: 557
Try this
NSString *imageString = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[yourDict objectForKey:@"ImageString"]];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]];
UIImage *image = [UIImage imageWithData:imageData];
Upvotes: 0
Reputation: 13222
The UIImage
method imageNamed:
is used to load image present locally. In your case you need to download the image from the URL. There are various ways to do this based on requirement.
Easiest one is,
//URL encode the string
NSString *url = [[result objectForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
imageview.image = [UIImage imageWithData:imageData];
This will download the image synchronously and on the main thread(if called from main thread). You want to do this asynchronously on background thread. Usually dispatch_async
(GCD) is used for doing this.
Hope that helps!
Upvotes: 1
Reputation: 859
If 'imagee' returned exact url , then fetch the image from web service should be like this.
imageview.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:@"image"]]]];
Upvotes: 0