gpt4 book ai didi

ios - 如何显示没有图像 UIWebImageView

转载 作者:行者123 更新时间:2023-11-29 03:23:11 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com