chavo218
chavo218

Reputation: 1

UIImageView Takes Time To Load Image

I wanted to download image from internet from particular link Downloading this image take place in other class which is Singleton class name moreAppInternet.m

My moreAppInterNet.m file is as follow

//finishedImgDling is bool

//mutableData is NSMutableData

//urlConnection is NSURLConnection

//appimage is UIImage

//imageUrl is NSString Containing Link

 +(id)sharedManager
{

    static moreAppInterNet *sharedMyManager = nil;

    @synchronized(self) {

        if (sharedMyManager == nil)

            sharedMyManager = [[self alloc] init];
    }

    return sharedMyManager;
}


-(void)downloadImageFromUrl
{    
    finishedImgDling = NO;
        
    [self.mutableData setLength:0];
    
    self.urlConnection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.imageUrl]] delegate:self];
    
    
    while(!finishedImgDling) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }   
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    if (connection == self.urlConnection)
    {

        NSLog(@"imageDling");
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    if (connection == self.urlConnection)
    {

        if (self.mutableData == nil)
        {
            self.mutableData = [NSMutableData data];
        }
                
        [self.mutableData appendData:data];
    }
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    if (connection == self.urlConnection)
    {
        NSLog(@"image Downloaded");
        
        finishedImgDling = YES;
                
        [self setUrlConnection:nil];
        
        [self storeImage];
    }
}

-(void)storeImage
{

    [self setAppimage:nil];
    
    self.appimage = [UIImage imageWithData:self.mutableData];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];     
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *path =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"appImage.png"]];
          
    [fileManager createFileAtPath:path contents:UIImagePNGRepresentation(self.appimage) attributes:nil];

     NSLog(@"downloading and storing complete");
        
}

and my other ViewController is as follow from where I call that function


-(void)viewDidLoad
{

    [moreAppInterNet sharedManager];

    [self downloadImage];
}

-(void)downloadImage
{

     NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download_Image_from_other_class) object:nil];
        
    NSOperationQueue *que = [[NSOperationQueue alloc] init];
    
    [que addObserver:self forKeyPath:@"operations" options:0 context:NULL];
    
    [que addOperation:operation];
    
    [operation release];
    
    self.dlingQue = que;
    
    [que release];
}

-(void)download_Image_from_other_class
{

    [[moreAppInterNet sharedManager] downloadImageFromUrl];
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                         change:(NSDictionary *)change context:(void *)context
{

    if ([keyPath isEqualToString:@"operations"] && object == self.dlingQue)
    {

        if ([self.dlingQue operationCount] == 0)
        {

            self.imgView = [[moreAppInterNet sharedManager] appimage];

            //it takes lots of time to display image in UIImageView
            //Even though Above line get executed
        }
    }
    else {

        [super observeValueForKeyPath:keyPath ofObject:object
                               change:change context:context];
    }
}

The Problem is that self.imgView takes Lots of time to load the image approx. 5sec even though its downloaded completely.

Upvotes: 0

Views: 160

Answers (2)

user2706034
user2706034

Reputation: 11

The library I use for networking is AFNetworking

Upvotes: 1

user2706024
user2706024

Reputation: 11

I think your code downloads the image again when you request it.

You need to check if you have the file in downloadImageFromUrl.

Probably, you need more robust library to handle file downloading.

Upvotes: 0

Related Questions