Image storage as byte code locally from XML parsing & display if it is stored Otherwise go for parsing.

I am having an app in which at the time of launcing the app XML parsing is giving Main category from URL like hp, dell, etc...I am displaying it in the Tableview.

Then on click of particular cell i can get the detail of main category means its subcategory like http://www.dealsbell.com/findcoupon.php?store=hp

Here also i am getting data properly after parsing.

But my concern over here is, in ( http://www.dealsbell.com/findcoupon.php?store=hp ) this link i am getting images.

Each particular subcategory will have a same image. So i want to do something like that the image if first time loaded from the URL then it will display image from parsing otherwise i would like to store that image as its byte code in folder / file / in any way in my device on first parsing.

If once the image is stored to the particular way in my device next time when i will go to see the subcategory it will first check this image is stored locally to my device or not.

If yes then it should go to the particular location to fetch this local image & display it to each cell otherwise will parse & display image.

I hope you are getting, what i want to ask.

Please guide me, how can this be possible & what is the way to get result.

If any example or link you can suggest, then it will be more efficient to me.

Thanks in advance.

Answers


There are probably two ways to achive this.

  1. Get NSData out of Image and hold that in UserDefaults or database.
  2. Dump image in application folder and pick image from that place.

So whenever you try to load image for subcatogory check at one of place and if present use that. IF in case you have stored image and if any updated image comes,then remove previous copy and store new one.

-(void) SaveImageinDocumentWithName:(UIImage*) aUIImage Name:(NSString*) aName
{
    if(aUIImage)
    {
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSMutableString* str = [[NSMutableString alloc] initWithCapacity:300];
        [str appendString:documentsDirectory];
        [str appendString:@"/"];
        [str appendString:aName];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        [UIImagePNGRepresentation(aUIImage) writeToFile:str atomically:YES];

        if(str)
        {
            [str release];
            str = nil;

        }
        if(fileManager)
        {
            [fileManager release];
            fileManager = nil;
        }

        [pool release];
    }   

}

-- Getting saved image

 -(UIImage*)GetSavedImageWithName:(NSString*) aFileName
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSMutableString* str = [[NSMutableString alloc] initWithCapacity:300];
        [str appendString:documentsDirectory];
        [str appendString:@"/"];
        [str appendString:aFileName];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL success = [fileManager fileExistsAtPath:str];

        NSData *dataToWrite = nil;

        UIImage* image = nil; 

        if(!success)
        {

        }
        else 
        {
            image = [[UIImage alloc] initWithContentsOfFile:str];
        }
        if(dataToWrite)
        {
            [dataToWrite release];
            dataToWrite = nil;
        }

        if(str)
        {
            [str release];
            str = nil;
        }

        if(fileManager)
        {
            [fileManager release];
            fileManager = nil;
        }
        return image;
    }

Parse dealsbell.com/wp-content/uploads/mobile/hp.gif and take only hp.gif

NSString *strImage = [NSString stringWithFormat:@"%@",aBook.image];

UIImage* image = [self GetSavedImageWithName:strImage];

if(image) // This means Image exists
{
  // Do what you want
}
else
{
    NSURL *url4Image = [NSURL URLWithString:strImage]; 

    NSData *data = [NSData dataWithContentsOfURL:url4Image];

    if(data != NULL) 
    {
       image =[[UIImage alloc] initWithData:data]; 
       [self SaveImageinDocumentWithName:image Name:strImage]; // save for future ref.
    } 
    else
    { 
      image =[UIImage imageNamed:@"icon.png"]; 
    } 
 }

Need Your Help

SPSS Ruby Reader

ruby-on-rails-3 spss

I need to be able to read a spss .sav file from my RoR application. Does anyone know a gem or ruby code that can understand the format and syntax of a spss .sav file?

How to update windows forms application? If new Version is available means,put a notification alert to the user.

c# winforms

How to update windows forms application? If new Version is available means,put a notification alert to the user.