2011-04-19 52 views
0

從這個代碼AsyncImage從程序源獲取圖片?

#import "AsyncImageView.h" 
#import "ImageCache.h" 
#import "ImageCacheObject.h" 

static ImageCache *imageCache = nil; 

@implementation AsyncImageView 


- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
    } 
    return self; 
} 


- (void)drawRect:(CGRect)rect { 
    // Drawing code 
} 


- (void)dealloc { 
    [connection cancel]; 
    [connection release]; 
    [data release]; 
    [super dealloc]; 
} 

-(void)loadImageFromURL:(NSURL*)url { 
    if (connection != nil) { 
     [connection cancel]; 
     [connection release]; 
     connection = nil; 
    } 
    if (data != nil) { 
     [data release]; 
     data = nil; 
    } 

    if (imageCache == nil) 
     imageCache = [[ImageCache alloc] initWithMaxSize:2*1024*1024]; 

    [urlString release]; 
    urlString = [[url absoluteString] copy]; 
    UIImage *cachedImage = [imageCache imageForKey:urlString]; 
    if (cachedImage != nil) 
    { NSLog(@"get in"); 
     if ([[self subviews] count] > 0) 
     { 
      [[[self subviews] objectAtIndex:0] removeFromSuperview]; 
     } 
     UIImageView *imageView = [[[UIImageView alloc] initWithImage:cachedImage] autorelease]; 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 
     imageView.autoresizingMask = 
     UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     [self addSubview:imageView]; 
     imageView.frame = self.bounds; 
     [imageView setNeedsLayout]; 
     [self setNeedsLayout]; 
     return; 
    } 

#define SPINNY_TAG 5555 

    UIActivityIndicatorView *spinny = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    spinny.tag = SPINNY_TAG; 
    spinny.center = self.center; 
    [spinny startAnimating]; 
    [self addSubview:spinny]; 
    [spinny release]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection 
    didReceiveData:(NSData *)incrementalData { 
    if (data==nil) { 
     data = [[NSMutableData alloc] initWithCapacity:2048]; 
    } 
    [data appendData:incrementalData]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection { 
    [connection release]; 
    connection = nil; 

    UIView *spinny = [self viewWithTag:SPINNY_TAG]; 
    [spinny removeFromSuperview]; 

    if ([[self subviews] count] > 0) { 
     [[[self subviews] objectAtIndex:0] removeFromSuperview]; 
    } 

    UIImage *image = [UIImage imageWithData:data]; 

    [imageCache insertImage:image withSize:[data length] forKey:urlString]; 

    UIImageView *imageView = [[[UIImageView alloc] 
           initWithImage:image] autorelease]; 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 
    imageView.autoresizingMask = 
    UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [self addSubview:imageView]; 
    imageView.frame = self.bounds; 
    [imageView setNeedsLayout]; // is this necessary if superview gets setNeedsLayout? 
    [self setNeedsLayout]; 
    [data release]; 
    data = nil; 
} 

@end 

如果我想獲得從應用程序源畫面,如果網址是空的,我應該補充哪些代碼?

,這裏是從xyz.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier: 
          CellIdentifier]; 
    if (cell == nil) { 
     //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell = [self getCellContentView:CellIdentifier]; 
    } 
    else{ 
     AsyncImageView *oldImage = (AsyncImageView *)[cell.contentView viewWithTag:999]; 
     [oldImage removeFromSuperview]; 
    } 
    int index = [indexPath indexAtPosition: [indexPath length] - 1]; 

    //Get Picture 
    CGRect frame; 
    frame.size.width=50; frame.size.height=50; 
    frame.origin.x=10; frame.origin.y=0; 
    AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease]; 
    asyncImage.tag = 999; 

    NSString *string = [jsonPic objectAtIndex:index]; 
    NSString *url=[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSURL *imageURL = [NSURL URLWithString:url]; 
    if([string isEqualToString:@""]){ 
    NSLog(@"Not found"); 

更多的代碼,在這裏我不知道我怎樣才能從圖片來源

 AsyncImageView * NoImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease]; 
     NoImage.tag = 999; 
     NoImage.image = [UIImage imageNamed:@"bl-noImg.gif"]; 
     [cell.contentView addSubview:NoImage]; 
    } 
    else 
    { NSLog(@"image URL %@",imageURL); 
     [asyncImage loadImageFromURL:imageURL]; 
     [cell.contentView addSubview:asyncImage]; 

我可以從asyncImage

得到圖片

請幫助我或指導我做到這一點。謝謝 。 。 。 。 現在,這一切都做,這是我的結果代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier: 
          CellIdentifier]; 
    if (cell == nil) { 
     //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell = [self getCellContentView:CellIdentifier]; 
    } 
    else{ 
     AsyncImageView *oldImage = (AsyncImageView *)[cell.contentView viewWithTag:999]; 
     [oldImage removeFromSuperview]; 
    } 
    int index = [indexPath indexAtPosition: [indexPath length] - 1]; 

    //Get Picture 
    CGRect frame; 
    frame.size.width=50; frame.size.height=50; 
    frame.origin.x=10; frame.origin.y=0; 
    AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease]; 
    asyncImage.tag = 999; 

    NSString *string = [jsonPic objectAtIndex:index]; 
    if([string isEqualToString:@""]){ 
    //NSLog(@"Not found"); 
     UIImageView * NoImg = [[[UIImageView alloc] initWithFrame:frame] autorelease]; 
     NoImg.tag = 999; 
     [NoImg setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg-noImg" ofType:@"gif"]]]; 
     [cell.contentView addSubview:NoImg]; 
    } 
    else 
    { //NSLog(@"image URL %@",imageURL); 
     NSString *url=[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     NSURL *imageURL = [NSURL URLWithString:url]; 
     [asyncImage loadImageFromURL:imageURL]; 
     [cell.contentView addSubview:asyncImage]; 
    } 

謝謝大家:)

+0

那麼你不應該創建請求和呼叫連接(檢查URL)。你還需要一些圖片來顯示嗎?(然後你可以使用一些默認圖像,(也可以在url沒有返回任何圖像的情況下...沒有圖像,那麼你應該使用一些默認圖片) – Ravin 2011-04-19 03:43:09

+0

所以我應該創建if(檢查URL)在xyz.m中,對嗎? – crazyoxygen 2011-04-19 04:31:44

回答

0

如果要檢查空鏈接,那麼你應該打電話給loadImageFromURL甚至創建NSURL對象之前前檢查。如果它不是空的,那麼你應該創建NSURL對象並調用loadImageFromURL方法。

地方在你的代碼......從正在調用loadImageFromURL:

if(urlString !=nil || [urlString length]>0) 
    { 
    create NSURL object 
    now again check NSURL object whether its nil or not 
    we are checking it because if the urlString has incorrect url pattern then no 
NSURLObject would be created, so if there is no NSURLObject then we should not call 
your method. 
    if(NSURLObject !=nil) 
    { 
     call loadImageFromURL method and so on 
    } 
    else 
    { 
     //load some default image. which will convey no URL Found 
    } 

    } 
    else 
    { 
     //load some default image. which will convey no URL Found 
    } 

感謝,

+0

非常感謝, – crazyoxygen 2011-04-19 05:21:45

+0

我在上面添加了更多的代碼,請看看。 – crazyoxygen 2011-04-19 05:31:21

+0

我不確定你需要幫助嗎?是否有一些連接問題?圖像沒有被加載?或者還有其他的東西嗎?請提一下 – Ravin 2011-04-19 07:09:31