-2

How to get image from server by sending one parameter with url.

GET HTTPRequest has to be use to send request.

IOS_5555
  • 67
  • 2
  • 8

3 Answers3

4

You can get image from server with specific path of that image with image name.

UIImage* serverImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://example.com/image_name.png"]]];

then you can use serverImage anywhere you want.

Bhavin_m
  • 2,746
  • 3
  • 30
  • 49
2

Try to use this :

-(UIImage*)getImageFromURLwithUrl:(NSString*)imgURLStr
{
    NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imgURLStr]];
    NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
    UIImage *image = [UIImage imageWithData:imageData];

    return image;
}
Satish Azad
  • 2,302
  • 1
  • 16
  • 35
1

A pretty useful project that I use to load Images without blocking the UI is the SDWebImage. It adds an asynchronous category on UIImageView, enabling you to load an image with just one line of code:

[myImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Bersaelor
  • 2,517
  • 34
  • 58
  • It's u blocking the UI. You are updating your UI after download finished, you should use this asynchronously. And it's `SDWebImage`'s duty to update your UI on main thread. – Roger Sep 07 '15 at 05:56
  • Exactly, so the above code is not blocking the main thread since SDWebImage is downloading on a background thread and then updating on the main thread after it's done. – Bersaelor Sep 17 '15 at 10:21