作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 UIWebImageView(自定义类继承 UIImageView)它的工作正常,从 url 下载图像并显示它 webview,在某些情况下没有特定 url 的图像然后它显示空白,所以需要在那里显示没有图像, 如何找到它的返回图像。
@implementation UIWebImageView
#define kAnimationDuration 0.5
@synthesize animate;
#pragma mark -
#pragma mark Public Methods
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
imageData = [[NSMutableData alloc] init];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGFloat xCoord = (frame.size.width / 2.0f) - [activityIndicator frame].size.width;
CGFloat yCoord = (frame.size.height / 2.0f) - [activityIndicator frame].size.height;
[activityIndicator setFrame:CGRectMake(xCoord, yCoord, [activityIndicator frame].size.width, [activityIndicator frame].size.height)];
[activityIndicator setHidesWhenStopped:YES];
[self addSubview:activityIndicator];
}
return self;
}
- (id)initWithFrame:(CGRect)frame andUrl:(NSURL *)url animated:(BOOL)animated {
if (self = [self initWithFrame:frame]) {
[activityIndicator startAnimating];
animate = animated;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if (connection)
{
}
}
return self;
}
- (void)downloadImage:(NSURL *)url {
[activityIndicator startAnimating];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if (connection)
{
}
}
#pragma mark -
#pragma mark NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[imageData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[activityIndicator stopAnimating];
UIImage *downloadedImage = [[UIImage alloc] initWithData:imageData];
if (animate)
{
[self setAlpha:0];
[UIView beginAnimations:@"Animations" context:nil];
[UIView setAnimationDuration:kAnimationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[self setAlpha:100];
[UIView commitAnimations];
}
[self setImage:downloadedImage];
}
- (void)dealloc {
}
最佳答案
尝试检查 downloadedImage
是否为 nil,如果为 nil,则显示带有“No Photo”标题的标签或显示带有“No Photo”的图像。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[activityIndicator stopAnimating];
UIImage *downloadedImage = [[UIImage alloc] initWithData:imageData];
if (animate)
{
[self setAlpha:0];
[UIView beginAnimations:@"Animations" context:nil];
[UIView setAnimationDuration:kAnimationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[self setAlpha:100];
[UIView commitAnimations];
}
if (downloadedImage) {
[self setImage:downloadedImage];
}
else {
// no image downloaded, show "No Image" message
}
}
关于ios - 如何显示没有图像 UIWebImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20813159/
我正在使用 UIWebImageView(自定义类继承 UIImageView)它的工作正常,从 url 下载图像并显示它 webview,在某些情况下没有特定 url 的图像然后它显示空白,所以需要
我是一名优秀的程序员,十分优秀!