2012-02-22 15 views
0

から画像を取得し、私は負荷のために、このクラスを使用する一部の画像asincronusly私は少し問題を抱えているasyncimageview

#import <UIKit/UIKit.h> 
@interface AsyncImageView : UIView { 
    NSURLConnection* connection; 
    NSMutableData* data; 
} 
- (void)loadImageFromURL:(NSURL*)url; 
- (UIImage*) image; 
@end 


#import "AsyncImageView.h" 

@implementation AsyncImageView 


- (void)dealloc { 
    [connection cancel]; //in case the URL is still downloading 
} 

- (void)loadImageFromURL:(NSURL*)url { 
    //in case we are downloading a 2nd image 

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object 

} 

//the URL connection calls this repeatedly as data arrives 
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData { 

    if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; } 

    [data appendData:incrementalData]; 
} 

//the URL connection calls this once all the data has downloaded 
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection { 

    //so self data now has the complete image 
    connection=nil; 

    if ([[self subviews] count]>0) { 
     //then this must be another image, the old one is still in subviews 
     [[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also) 
    } 

    //make an image view for the image 
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]]; 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 

    [self addSubview:imageView]; 
    imageView.frame = self.bounds; 
    [imageView setNeedsLayout]; 

     [self setNeedsLayout]; 
     data=nil; 
} 

- (UIImage*) image { 
    UIImageView* iv = [[self subviews] objectAtIndex:0]; 
    return [iv image]; 
} 
@end 

このコードでこのロード用画像

AsyncImageView *asyncImage = [[AsyncImageView alloc] initWithFrame:frame]; 

asyncImage.tag = 999; 
NSURL *url = [NSURL URLWithString:indirizzoImmagine];  

[asyncImage loadImageFromURL:url]; 
[self.view addSubView:asyncImage] 

すべての作業が、私[cell.imageview setImage:async.image]にasyncImageを配置します。アプリのクラッシュ、私はuiimageview何もしてサブクラスを変更する必要があると思います...同じエラー

キャッチされない例外により「NSRangeException」、理由にアプリを終了: '*- [__ NSArrayI objectAtIndex:]:空の配列の境界を超えるインデックス0

イメージのみを取得するにはどうすればよいですか?あなたがここにImageViewのをイメージ

[imageView.imageCache imageForKey:str_ImgUrl]; 

を得ることができ、次のコードを使用してのImageCacheを合成AsyncImageViewクラスで

答えて

関連する問題