2012-08-10 76 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