How to get image from server using NSUrlConnection [duplicate]

依然范特西╮ 提交于 2019-12-05 08:16:22

问题


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

GET HTTPRequest has to be use to send request.


回答1:


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.




回答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;
}



回答3:


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"]];


来源:https://stackoverflow.com/questions/17365135/how-to-get-image-from-server-using-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!