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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:22:33  来源:igfitidea点击:

XCode: String URL to Image

iosobjective-cxcodeimageurl

提问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]]];

回答by delannoyk

I suggest you to use some kind of Image loader like this one.

我建议你使用某种像这样的图像加载器。

It will load the image in background and set it to an ImageView when it's ready. It gives a way better experience as user.

它将在后台加载图像,并在准备好后将其设置为 ImageView。它为用户提供了更好的体验。