如何使用NSUrlConnection从服务器获取图像

如何通过使用url发送一个参数从服务器获取图像。

GET HTTPRequest必须用于发送请求。

您可以从服务器获取图像的具体path与图像名称的图像。

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

那么你可以在任何你想要的地方使用serverImage

尝试使用这个:

 -(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; } 

SDWebImage是一个非常有用的项目,用于加载图像而不会阻塞UI。 它在UIImageView上添加了一个asynchronous类别,使您只需一行代码即可加载图像:

 [myImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];