0

我有一个应用程序,在启动应用程序 XML 解析时,它从 hp、dell 等 URL 提供 Main 类别......我在 Tableview 中显示它。

然后单击特定单元格,我可以获得主要类别的详细信息,表示其子类别,如http://www.dealsbell.com/findcoupon.php?store=hp

在这里,我也在解析后正确获取数据。

但我在这里担心的是,在(http://www.dealsbell.com/findcoupon.php?store=hp)这个链接中,我正在获取图像。

每个特定的子类别都将具有相同的图像。所以我想做这样的事情,如果第一次从 URL 加载图像,那么它将显示解析的图像,否则我想将该图像作为其字节码存储在文件夹/文件/以任何方式在我的设备中第一次解析.

如果一旦图像以特定方式存储在我的设备中,下次我将去查看子类别时,它将首先检查此图像是否存储在我的设备本地。

如果是,那么它应该去特定位置获取这个本地图像并将其显示到每个单元格,否则将解析并显示图像。

我希望你得到,我想问的。

请指导我,这怎么可能以及获得结果的方法是什么。

如果您可以建议任何示例或链接,那么它对我来说会更有效。

提前致谢。

4

2 回答 2

2

可能有两种方法可以实现这一目标。

  1. 从 Image 中获取 NSData 并将其保存在 UserDefaults 或数据库中。
  2. 将图像转储到应用程序文件夹中并从该位置选择图像。

因此,每当您尝试在某个位置加载图像以进行子类别检查时,如果存在,请使用该图像。如果您存储了图像并且如果有任何更新的图像出现,则删除以前的副本并存储新的。

-(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];
    }   

}

-- 获取保存的图像

 -(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;
    }
于 2012-05-24T06:11:32.070 回答
1

解析 dealbell.com/wp-content/uploads/mobile/hp.gif 并仅获取 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"]; 
    } 
 }
于 2012-05-24T07:14:36.893 回答