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.
我试图从其URL加载UIImage与完成和占位符图像,但既不加载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) {}
];
4 个解决方案
#1
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,如果网址不正确,也可以设置占位符图像。在tableview或集合视图中加载图像时,这会更有效。
Import 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下载它
#2
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文件中写下代码。确保图像的网址不应为零。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
#3
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,其余部分将由此处理。包括缓存。
#4
I found the problem
我发现了这个问题
My code is working correctly.
我的代码工作正常。
The problem was somewhere in my code , the ImageView
was nil
. Thanks all
问题出在我的代码中,ImageView是零。谢谢大家
#1
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,如果网址不正确,也可以设置占位符图像。在tableview或集合视图中加载图像时,这会更有效。
Import 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下载它
#2
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文件中写下代码。确保图像的网址不应为零。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
#3
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,其余部分将由此处理。包括缓存。
#4
I found the problem
我发现了这个问题
My code is working correctly.
我的代码工作正常。
The problem was somewhere in my code , the ImageView
was nil
. Thanks all
问题出在我的代码中,ImageView是零。谢谢大家