Download image asynchronously

后端 未结 8 923
滥情空心
滥情空心 2020-12-03 19:46

I need to download an image from the web and display it in an ImageView. Presently I am using SDWebImage (It is an asynchronous image downloader wi

8条回答
  •  醉话见心
    2020-12-03 20:23

    //JImage.h
    
    #import 
    
    
    @interface JImage : UIImageView {
    
        NSURLConnection *connection;
    
        NSMutableData* data;
    
        UIActivityIndicatorView *ai;
    }
    
    -(void)initWithImageAtURL:(NSURL*)url;  
    
    @property (nonatomic, retain) NSURLConnection *connection;
    
    @property (nonatomic, retain) NSMutableData* data;
    
    @property (nonatomic, retain) UIActivityIndicatorView *ai;
    
    @end
    
    
    
    //JImage.m
    
    #import "JImage.h"
    
    @implementation JImage
    @synthesize ai,connection, data;
    
    -(void)initWithImageAtURL:(NSURL*)url {
    
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
        [self setContentMode:UIViewContentModeScaleToFill];
    
        if (!ai){
    
            [self setAi:[[UIActivityIndicatorView alloc]   initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]]; 
    
            [ai startAnimating];
    
            [ai setFrame:CGRectMake(27.5, 27.5, 20, 20)];
    
            [ai setColor:[UIColor blackColor]];
    
            [self addSubview:ai];
        }
        NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];    
    }
    
    
    - (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    
    if (data==nil) data = [[NSMutableData alloc] initWithCapacity:5000];
    
    [data appendData:incrementalData];
    
    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[data length]];
    
    NSLog(@"resourceData length: %d", [resourceLength intValue]);
    
    }
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    {
        NSLog(@"Connection error...");
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
        [ai removeFromSuperview];
    
    }
    - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 
    {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
        [self setImage:[UIImage imageWithData: data]];
    
        [ai removeFromSuperview];   
    }
    @end
    
    
    
    //Include the definition in your class where you want to use the image
    -(UIImageView*)downloadImage:(NSURL*)url:(CGRect)frame {
    
    JImage *photoImage=[[JImage alloc] init]; 
    
        photoImage.backgroundColor = [UIColor clearColor]; 
    
       [photoImage setFrame:frame];
    
       [photoImage setContentMode:UIViewContentModeScaleToFill]; 
    
        [photoImage initWithImageAtURL:url];
    
        return photoImage;
    }
    
    
    //How to call the class
    
    UIImageView *imagV=[self downloadImage:url :rect]; 
    
    //you can call the downloadImage function in looping statement and subview the returned  imageview. 
    //it will help you in lazy loading of images.
    
    
    //Hope this will help
    

提交回复
热议问题