0

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

当用户访问另一个 url 时,我将此图像存储在本地,该图标正在替换为以前的图标,以及如何替换此图标以重复使用,请帮助我。

谢谢你!

        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;
4

2 回答 2

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];
       }
    }
于 2012-08-31T11:48:33.360 回答
0

对于这种情况,您应该缓存下载的图像并为下一个请求恢复相同的图像。

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

于 2012-08-31T11:39:55.893 回答