2011-04-19 55 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