从 iOS 中的 URL 加载 UIImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30096806/
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
Load UIImage from URL in iOS
提问by Ahd Radwan
I am trying to load a UIImage
from its URL with completion
and placeholder
image , but neither the UIImage
nor the placeholder UIImage
are loaded.
我试图UIImage
从它的 URL加载 acompletion
和placeholder
image ,但既不加载UIImage
也不UIImage
加载占位符。
Here is my code:
这是我的代码:
NSURL *url = [NSURL URLWithString:@"http://assets.gearlive.com/tvenvy/blogimages/stewiegriffin.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"bg.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
imageView.image = image;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {}
];
回答by Ashok Londhe
Try this code to get image...
试试这个代码来获取图像...
Code:
代码:
NSURL *url = [NSURL URLWithString:@"http://www.fnordware.com/superpng/pnggrad16rgb.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
[self.imageview1 setImage:image];
Note:i have used test url. you can use your url.
注意:我使用了测试网址。你可以使用你的网址。
Update :Swift 3.0 Code:
更新:Swift 3.0 代码:
let url = URL(string: "http://www.fnordware.com/superpng/pnggrad16rgb.png")
do {
let data = try Data(contentsOf: url!)
var image = UIImage(data: data)
self.imageView.image = image
print(image)
} catch {
}
Note :In Xcode version 7.1 and above you need to set ATS (App Transport Security). To set ATS you need write below code in info.plist file. Make sure url of image should not be nil.
注意:在 Xcode 7.1 及以上版本中,您需要设置 ATS(App Transport Security)。要设置 ATS,您需要在 info.plist 文件中编写以下代码。确保图像的 url 不应该为零。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
回答by Akshay Karanth
Use AFnetworking
where you can set placeholder image also if the url is not correct. This is more efficient when you are loading images in a tableview or a collection view.
AFnetworking
如果 url 不正确,也可以在可以设置占位符图像的地方使用。当您在 tableview 或 collection view 中加载图像时,这会更有效。
Import UIImageView+AFNetworking.h
进口 UIImageView+AFNetworking.h
#import "UIImageView+AFNetworking.h"
and us the following code:
和我们下面的代码:
NSString *url1=@"http://www.fnordware.com/superpng/pnggrad16rgb.png";
[self.image setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
You can download it from gitHub
你可以从gitHub下载
回答by ShahiM
Check out AsyncImageView
. Its the best async image loader AFAIK. all you have to do is set the image URL to the imageview and the rest is taken care of. including caching.
退房AsyncImageView
。它是最好的异步图像加载器 AFAIK。您所要做的就是将图像 URL 设置为 imageview,其余的就都搞定了。包括缓存。
回答by Ahd Radwan
I found the problem
我发现了问题
My code is working correctly.
我的代码工作正常。
The problem was somewhere in my code , the ImageView
was nil
.
Thanks all
问题出在我的代码中的某个地方,ImageView
是nil
. 谢谢大家