ふるすたっくえんじにあっぽい人の日記

ASP .NET MVC (C#)、.NET Framework、iOS (Objective-c) アプリ、Androidアプリ (Java)、AWS、たまにLAMPとかプロジェクトマネジメントあたりのお話

【Objective-c】LazyImageView

作った。うるとらはいぱーてきとーw
テストはしてないので、動作保証はしません。
見た目上は動いてる
キャッシュもさせてないw

LazyImageView.h

@interface LazyImageView : UIImageView

/*!
 画像読み込みを開始する
 @param url URL
 */
- (void)startLoadImage:(NSString *)url;

/*!
 画像をリロードする
 */
- (void)reloadImage;

/*!
 読み込みを中断する
 */
- (void)stopLoadImage;

@end

LazyImageView.m

#import "LazyImageView.h"

@interface LazyImageView() <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

@property (nonatomic) UIActivityIndicatorView *indicatorView;

@property (nonatomic) NSString *url;

@property (nonatomic) NSURLConnection *connection;
@property (nonatomic) NSInteger statusCode;
@property (nonatomic) NSMutableData *receivedData;

@end

@implementation LazyImageView

- (void)startLoadImage:(NSString *)url
{
    if (self.image == nil) {
        self.url = url;
        
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:self.frame];
        [self addSubview:self.indicatorView];
        
        [self reloadImage];
    }
}

- (void)reloadImage
{
    [self stopLoadImage];
    
    self.backgroundColor = [UIColor lightGrayColor];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self.indicatorView startAnimating];
    
    self.image = nil;
    self.receivedData = [NSMutableData data];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [self.connection start];
    [self performSelector:@selector(onConnectionTimeout) withObject:nil afterDelay:20];
}

- (void)stopLoadImage
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.indicatorView stopAnimating];
    if (self.connection != nil) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(onConnectionTimeout) object:nil];
        [self.connection cancel];
        self.connection = nil;
    }
}

- (void)dealloc
{
    [self stopLoadImage];
}

- (void)layoutSubviews
{
    self.indicatorView.center = CGPointMake(self.width / 2, self.height /2);
}

#pragma mark - NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.receivedData.length = 0;
    
	self.statusCode = ((NSHTTPURLResponse *)response).statusCode;
	if (self.statusCode != 200) {
        [self onEndConnection:NO];
	}
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self onEndConnection:NO];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self onEndConnection:YES];
}

- (void)onConnectionTimeout
{
    [self onEndConnection:NO];
}

- (void)onEndConnection:(BOOL)isSuccess
{
    [self stopLoadImage];
    self.image = isSuccess ? [UIImage imageWithData:self.receivedData] : nil;
}