如何显示没有图像的UIWebImageView

时间:2013-12-28 09:51:27

标签: ios uiimageview

我是uisng 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 {

}

1 个答案:

答案 0 :(得分:1)

尝试检查downloadedImage是否为零,如果它为零,则显示带有“无照片”标题的标签或显示带“无照片”的图像。

- (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
    }

}
相关问题