How to get image from server using NSUrlConnection

2020-06-30 03:57发布

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

GET HTTPRequest has to be use to send request.

3条回答
做个烂人
2楼-- · 2020-06-30 04:21

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.

查看更多
狗以群分
3楼-- · 2020-06-30 04:27

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"]];
查看更多
Evening l夕情丶
4楼-- · 2020-06-30 04:41

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;
}
查看更多
登录 后发表回答