从iOS中的URL加载UIImage [英] Load UIImage from URL in iOS
本文介绍了从iOS中的URL加载UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用completion
和placeholder
图像从其URL加载UIImage
,但是UIImage
和占位符UIImage
均未加载.
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.
这是我的代码:
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) {}
];
推荐答案
使用AFnetworking
,如果url不正确,您还可以在其中设置占位符图像.在表格视图或集合视图中加载图像时,这种方法会更有效.
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.
导入UIImageView+AFNetworking.h
#import "UIImageView+AFNetworking.h"
和我们下面的代码:
NSString *url1=@"http://www.fnordware.com/superpng/pnggrad16rgb.png";
[self.image setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
您可以从 gitHub
这篇关于从iOS中的URL加载UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文