2013-07-06 152 views
2

我已經使用給here的代碼來保存和載入圖像負荷文檔目錄保存圖像

,當我在一個視圖控制器一起使用它,它工作正常,但是當我使用saveImage方法在一個視圖控制器並嘗試加載圖像中的圖像返回空白另一個視圖控制器...

鑑於控制器A我用下面的保存圖像

- (void)saveImage: (UIImage*)image 
{ 
    NSLog(@"saveimage called"); 

    if (image != nil) 
    { 
     NSLog(@"Image not null"); 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString* path = [documentsDirectory stringByAppendingPathComponent: 
          @"test.png" ]; 
     NSData* data = UIImagePNGRepresentation(image); 
     [data writeToFile:path atomically:YES]; 
     QrImageView.image = nil; 
    } 
} 

而且在視圖控制器說B I很裝載圖片使用..

- (UIImage*)loadImage 
{ 
    NSError *error; 

    NSFileManager *fileMgr = [NSFileManager defaultManager]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
         @"test.png" ]; 
    UIImage* image = [UIImage imageWithContentsOfFile:path]; 

    // Write out the contents of home directory to console 
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

    return image; 
} 

我也得在控制檯中的文件的內容,但我不明白爲什麼圖像是空白

2013-07-06 14:13:19.750 YouBank[500:c07] Documents directory: (
    "test.png" 
) 

任何想法,我做錯了什麼......

EDIT: 

在viewDidload方法的視圖控制器B中,我執行以下操作:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    ImageView.image=[self loadImage]; 
    ImageName.text = @"Cash Withdrawal"; 
} 
+0

是對UIImage的實例零? – Stavash

+0

不,它不是零 – Audi

+0

似乎問題是顯示UIImage後加載而不是加載數據本身。您能否顯示您使用的代碼以便在屏幕上顯示? – Stavash

回答

6

問題是你只是在viewDidLoad上用UIImage更新你的UIImageView。從文件系統獲得更新後(最好在後臺線程中),您需要添加更新。因此,這將是這個樣子:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{ 
    UIImage *loadedImage = [self loadImage]; 
    dispatch_async(dispatch_get_main_queue(),^{ 
     ImageView.image = loadedImage; 
    }); 
}); 

我也建議使用的名字開始與實例名稱小寫字符,所以你應該重新命名的ImageView - imageWithContentsOfFile後> ImageView的

+0

所以生病在我的viewDidLoad中使用這個.... – Audi

+0

所以,讓我瞭解情況 - 你從loadImage(而不是nil)獲得UIImage,將其分配給ImageView(這是一個IBOutlet嗎? ?)仍然沒有看到圖像? – Stavash

+0

是的......我甚至在mainqueue中添加了這個ImageName.text = @「dnsdjkvn」,文本得到了更新..但圖像仍然是空的 – Audi

2

試試這個

//Document Directory 
    #define kAppDirectoryPath NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 


    #pragma mark - File Functions - Document/Cache Directory Functions 
    -(void)createDocumentDirectory:(NSString*)pStrDirectoryName 
    { 
     NSString *dataPath = [self getDocumentDirectoryPath:pStrDirectoryName]; 

     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
      [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:NULL]; 
    } 

    -(NSString*)getDocumentDirectoryPath:(NSString*)pStrPathName 
    { 
     NSString *strPath = @""; 
     if(pStrPathName) 
      strPath = [[kAppDirectoryPath objectAtIndex:0] stringByAppendingPathComponent:pStrPathName]; 

     return strPath; 
    } 

當你寫的照片

 [self createDocumentDirectory:@"MyPhotos"]; 
     NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:strImageName]; 
     [UIImagePNGRepresentation(imgBg.image) writeToFile:pngPath atomically:YES]; 

當你得到的文件
編輯

NSError *error = nil; 
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[FunctionManager getDocumentDirectoryPath:@"MyPhotos"] error:&error]; 
    if (!error) { 
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"]; 
     NSArray *imagesOnly = [dirContents filteredArrayUsingPredicate:predicate]; 
      for (int i=0;i<[imagesOnly count]; i++) { 
       [arrSaveImage addObject:[imagesOnly objectAtIndex:i]]; //arrSaveImage is Array of image that fetch one by one image and stored in the array 
      } 
} 


    NSString *strPath=[self getDocumentDirectoryPath:@"MyPhotos"]; // "MyPhotos" is Your Directory name 
    strPath=[NSString stringWithFormat:@"%@/%@",strPath,[arrSaveImage objectAtIndex:i]]; //You can set your image name instead of [arrSaveImage objectAtIndex:i] 
    imgBackScroll.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:strPath]]; 

它可以幫助你。

+0

你能告訴我什麼是arrSaveImage? – Audi

+0

看到我編輯的答案 –

+0

它只是圖像名稱,我的要求是多一個圖像,所以我用上面的代碼,並把它放在for循環獲取一個圖像 –