XCode:图像的字符串 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16481166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode: String URL to Image
提问by Kurt Russell
How can I convert image from URL (String) to Image in XCode (Objective-C)? I have an images in URL. And I want to make the Array with this images but how images. Than I can read they in my IPhone aplicatioon. Thanks for help!
如何将图像从 URL(字符串)转换为 XCode(Objective-C)中的图像?我在 URL 中有一个图像。我想用这些图像制作数组,但如何制作图像。我可以在我的 iPhone 应用程序中阅读它们。感谢帮助!
回答by Marcin Kuptel
NSData *data = [NSData dataWithContentsOfURL : url];
UIImage *image = [UIImage imageWithData: data];
Just remember that -dataWithContentsOfURL:is a synchronous call and should not be performed on the main thread.
请记住-dataWithContentsOfURL:是一个同步调用,不应在主线程上执行。
回答by Thilina Chamin Hewagama
This is how to do it,
这是怎么做的,
NSString *imageUrlString = @"";
NSURL *url = [NSURL URLWithString:imageUrlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
回答by Girish
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]];