2012-08-10 58 views
2

这里我从url下载图像并保存到缓存目录中,我们从缓存路径中获取图像,但它的结果是获取image.is有任何替代解决方案来获取图像从将localPathios UIImage:imageWithContentsOfFile碰撞

这里我共享代码即可获得从localPath具有图像:

NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *path = [NSString stringWithFormat:@"%@/%@", cachePath,[[imageurl componentsSeparatedByString:@"/"]lastObject]]; 
UIImage *image = [UIImage imageWithContentsOfFile:path]; 
+0

NS记录你的路径并检查它是否正确。 – Apurv 2012-08-10 12:46:03

+0

我不知道什么“被击中”意味着你可以提供缓存路径和路径的NSLog?并提供任何例外。通话结束后图像无效吗? – 2012-08-10 12:47:21

+0

感谢您的回复,我在tableview中显示图像,每当单元格重用图像从本地路径采取,以便其加载图像 – dineshprasanna 2012-08-10 12:57:14

回答

0

你应该做的是处理图像的下载和高速缓存的NSObject类。实施例

接口文件

 #import <Foundation/Foundation.h> 

      @interface ImageCache : NSObject 
      { 
      UIImage *background; 
      } 
    @property (nonatomic,retain) UIImage *background;   
      -(void)getImages; 
      -(void) storeImages; 
      -(void) clearCache; 

    @end 

实施

@implementation ImageCache 

     @synthesize background; 


      - (void) cacheImage: (NSString *) ImageURLString : (NSString *)imageName 
      { 
       NSURL *ImageURL = [NSURL URLWithString: ImageURLString]; 

       // Generate a unique path to a resource representing the image you want 

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
       NSString *docDir = [paths objectAtIndex: 0]; 
       NSString *docFile = [docDir stringByAppendingPathComponent: imageName]; 

       // Check for file existence 
       if(![[NSFileManager defaultManager] fileExistsAtPath: docFile]) 
       { 
        // The file doesn't exist, we should get a copy of it 

        // Fetch image 
        NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL]; 

        UIImage *image = [[UIImage alloc] initWithData: data]; 

        // Is it PNG or JPG/JPEG? 
        // Running the image representation function writes the data from the image to a file 
        if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound) 
        { 

         [UIImagePNGRepresentation(image) writeToFile: docFile atomically: YES]; 

        } 
        else if 
        (
          [ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound || 
          [ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound 
        ) 
        { 
         [UIImageJPEGRepresentation(image, 100) writeToFile: docFile atomically: YES]; 
        } 
       } 
      } 

      - (UIImage *) getCachedImage : (NSString *)imageName 
      { 

       NSArray *paths = NSSearchPathForDirectoriesInDomains 
       (NSDocumentDirectory, NSUserDomainMask, YES); 
       NSString *documentsDirectory = [paths objectAtIndex:0]; 
       NSString* cachedPath = [documentsDirectory stringByAppendingPathComponent:imageName]; 

       UIImage *image; 

       // Check for a cached version 
       if([[NSFileManager defaultManager] fileExistsAtPath: cachedPath]) 
       { 
        image = [UIImage imageWithContentsOfFile: cachedPath]; // this is the cached image 
       }else 
       { 
        NSLog(@"Error"); 
       } 

       return image; 
      } 

     -(void)getImages 
     {  
//I just googled background image and took first image I found for this example 
     NSString *backgroundURL = @"http://www.dvd-ppt-slideshow.com/images/ppt-background/background-3.jpg"; 
     [self cacheImage:backgroundURL: @"Background"]; 

     } 

    -(void) clearCache 
    { 
    //If you ever need to clear the cache for whatever reason, good to have a method to handle it  
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *docDir = [paths objectAtIndex: 0]; 
     NSString *docFile1 = [docDir stringByAppendingPathComponent: @"Background"]; 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     [fileManager removeItemAtPath:docFile1 error:NULL]; 

    } 

    -(void) storeImages 
    { 
     background = [self getCachedImage:@"Background"]; 
    } 

@end 

在示例我给我只是用一个图像。但你确实知道这个想法。现在,如果你想下载和存储该图像。只需在您的ViewController中导入ImageCache类,然后致电

ImageCache *cacheMe = [[ImageCache alloc] init]; 
    [cache getImages]; 
    [cacheMe storeImages]; 
    UIImageView *image = [[UIImageView alloc] initWithImage:[cacheMe background]]; 

而这就是您所需要做的。如果你有一个致力于缓存的课程,那就更容易了。希望帮助!

编辑:更新忘了两行代码。我自己测试了一下,现在应该可以正常工作了

+0

谢谢,实际上这种检索过程只是我使用.my问题是从本地路径获取图像并将其显示在tableview中,从本地路径获取图像是st-rucking.how我可以流利地滚动tableview,你可以有任何想法 – dineshprasanna 2012-08-13 10:37:47

+0

我不完全确定是什么你的意思是通过打击?你只是想要一个包含不同图像的桌面视图?如果是这样,我的代码应该工作正常。只需设置一个tableview,并给每个单元格一个图像。或者如果你想要文本和图像,那么你将需要创建自己的自定义表格单元格 – AdamM 2012-08-13 12:26:47