如何从文档文件夹中获取保存的图像,并在下次访问url时显示图像,而不是从parsecd图像

时间:2012-08-31 11:35:28

标签: iphone ios

我有一个应用程序,我想在本地存储来自在线图像路径(网址)的图标图像。

我将此图片存储在本地,当用户访问该图标替换为之前的另一个网址以及如何替换此图标以供重复使用时,PLZ帮助我。

谢谢你!

        NSString *strImage = [NSString stringWithFormat:@"%@",aBook.image];
        NSURL *url4Image = [NSURL URLWithString:strImage];
        NSData *data = [NSData dataWithContentsOfURL:url4Image];
        if(data !=NULL)
        {
            imageView =[[UIImage alloc] initWithData:data];
            if(imageView == NULL)
            {
                imageView =[UIImage imageNamed:@"dealsbell_lo.png"];    //  NoImage.png

            }
        }
        NSData *imageData = UIImagePNGRepresentation(imageView);
        NSString *imageName = [NSString stringWithFormat:@"image.png"];
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
        [imageData writeToFile:fullPathToFile atomically:YES];

        cell.imageView.image = imageView;

2 个答案:

答案 0 :(得分:0)

对于这种情况,您应该缓存下载的图像并继续下一个请求。

我认为您应该使用JMImageCache框架。

答案 1 :(得分:0)

这样做:

    NSString *imageName = [NSString stringWithFormat:[NSString stringWithFormat:@"image_%d.png",indexPath.row]];//change name of image
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    if([[NSFileManager defaultManager] fileExistsAtPath:fullPathToFile])
    {
        NSData *imgData  = [NSData dataWithContentsOfFile:fullPathToFile];
        if(imgData){
        UIImage *img = [UIImage imageWithData:imgData];
        cellimageView.image = img;
        }
    }
    else
    {
       NSString *strImage = [NSString stringWithFormat:@"%@",aBook.image];
       NSURL *url4Image = [NSURL URLWithString:strImage];
       NSData *data = [NSData dataWithContentsOfURL:url4Image];
       if(imgData){
       UIImage *img = [UIImage imageWithData:imgData];
       cellimageView.image = img;
       [data writeToFile:fullPathToFile atomically:YES];
       }
    }