1

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.

sathya
  • 135
  • 1
  • 3
  • 19

7 Answers7

2

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];
sathya
  • 135
  • 1
  • 3
  • 19
1

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!

Amar
  • 13,202
  • 7
  • 53
  • 71
  • still i could get only the empty imageview. – sathya Sep 17 '13 at 09:59
  • @sathya Are you sure `[result objectForKey:@"image"]` returns a valid URL or any URL at all? Can you log the value and check? – Amar Sep 17 '13 at 10:01
  • yes, I could get the .jpg file printing on the console (eg., image is xxxxxxxxx/xxxxx.xxxxxxx.jpg), checked it in the browser, it returns the exact image needed to be displayed on the imageview. – sathya Sep 17 '13 at 10:09
  • still the same, will be that due to size of the imageview i allocated, i just assigned the size of width:72 and height:70 – sathya Sep 17 '13 at 10:16
  • 1
    @sathya Is your `IBOutlet` connected correctly to `imageview` variable? Also is `imageData` equal to `nil` when you download image? – Amar Sep 17 '13 at 10:17
  • @sathya The frame should not be an issue because even then it should atleast display the image. Can you check `imageData` value if it comes out to be `nil`? Is it possible to share the image URL? – Amar Sep 17 '13 at 10:21
  • sorry for the image URL, yes the imageData value is printed to be 'null' in console – sathya Sep 17 '13 at 10:30
  • 1
    @sathya That means the URL is not downloading data. Something wrong with the URL. – Amar Sep 17 '13 at 10:37
  • Amar, i'm getting 'null' in console for imageData, is there any problem in this value? – sathya Sep 17 '13 at 10:38
  • @sathya Yes, the API will return `nil` when it is unable to download data. – Amar Sep 17 '13 at 10:40
  • 1
    Amar, i have found the solution for my problem faced a day before, the 'null' value of imageData is b'coz of missing of appending the 'http' with the required url fetched. posted the answer for my question... – sathya Sep 19 '13 at 07:34
0

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"]]]];
wesley
  • 859
  • 5
  • 15
0

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];
Prasad_R
  • 557
  • 2
  • 11
0

.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;
Arun
  • 3,406
  • 4
  • 30
  • 55
Kasirajan M
  • 135
  • 9
0

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.

Jeremy
  • 366
  • 5
  • 15
-1

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

Community
  • 1
  • 1
Imran
  • 1,715
  • 2
  • 20
  • 42