2013-07-29 230 views
0

我想顯示保存在文件夾的問題圖像是返回文件名,但它不會在圖像查看如何在iPhone的顯示從文件夾圖像圖像ViewI

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

      NSString*[email protected]"nicej"; 

      patientlastName=[test stringByReplacingOccurrencesOfString:@" " withString:@""]; 
     NSString * filePath = [NSString stringWithFormat:@"%@/Documents//%@.png", NSHomeDirectory(),patientlastName]; 


     BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 
     if (fileExists == 0) 
     { 
      NSLog(@"File is Not exists"); 

      [email protected]"File Not Exists"; 

     } 

     NSString *userfile=[NSString stringWithFormat:@"%@",patientlastName]; 
     appDelegate.imageURL=userfile; 

      PictureDisplayViewController *targetController = [[PictureDisplayViewController alloc] initWithNibName:@"PictureDisplayViewController" bundle:nil]; 
     targetController.modalPresentationStyle = UIModalPresentationFullScreen; 
     [self.splitViewController presentViewController:targetController animated:YES completion:nil]; 

顯示圖像在PictureDisplayViewController

  imageView.image=[UIImage imageNamed:@"nicej.png"]; 

回答

1

試試這個代碼:

圖像保存在這條道路

NSFileManager *fileMan = [NSFileManager defaultManager]; 

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

    NSString *str= [NSString stringWithFormat:@"%@.png",patientlastName]; 
    NSString *Image_filePath=[documentsDirectory stringByAppendingPathComponent:str]; 
NSData *imagedata=UIImagePNGRepresentation(yourimage); 
[fileMan createFileAtPath:[Image_filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] contents:imagedata attributes:nil]; 

獲取圖像路徑

NSString* fileName = [NSString stringWithFormat:@"%@.png",patientlastName} 
     NSArray *arrayPaths = 
     NSSearchPathForDirectoriesInDomains(
              NSDocumentDirectory, 
              NSUserDomainMask, 
              YES); 
     NSString *path = [arrayPaths objectAtIndex:0]; 
     NSString* pdfFileName = [path stringByAppendingPathComponent:fileName]; 

     UIImage *image1=[UIImage imageWithContentsOfFile:pdfFileName]; 
     imageView.image=image1; 
+0

在ImageView的,這也沒有顯示圖像 – user2240329

+0

檢查我更新的答案 –

+0

因爲你給了我現在的儲蓄文件,但它不會加載可能是什麼問題 – user2240329

0
//suppose imgpath is path of your image in doument directory then use these two lines of code. 

NSData *imgData = [NSData dataWithContentsOfFile:imgPath]; 

imageView.image = [[UIImage alloc] initWithData:imgData]; 
0

PictureDisplayViewController,您加載與imageNamed方法圖像實際上不起作用,直到你有圖像與同名到您的捆綁。 您可以加載圖像等作爲跟隨...

PictureDisplayViewController你已經有路徑,圖像比

imageView.image= [UIImage imageWithContentsOfFile:filePathWithName]; 
+0

我做了像PictureDisplayViewController kirthi,但它不工作 – user2240329

0

我修改代碼:

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

       NSString*[email protected]"nicej"; 

       patientlastName=[test stringByReplacingOccurrencesOfString:@" " withString:@""]; 
      NSString * filePath = [NSString stringWithFormat:@"%@/Documents//%@.png", NSHomeDirectory(),patientlastName]; 


      BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 
      if (fileExists == 0) 
      { 
       NSLog(@"File is Not exists"); 

       [email protected]""; 

      } 
      else 
      { 
       appDelegate.imagePath=filePath; 
      } 

       PictureDisplayViewController *targetController = [[PictureDisplayViewController alloc] initWithNibName:@"PictureDisplayViewController" bundle:nil]; 
      targetController.modalPresentationStyle = UIModalPresentationFullScreen; 
      [self.splitViewController presentViewController:targetController animated:YES completion:nil]; 

在PictureDisplayViewController

if(appDelegate.imagePath) 
{ 
    UIImage *image=[UIImage imageWithContentsOfFile:appDelegate.imagePath]; 
} 
else 
{ 
    imageView.image=[UIImage imageNamed:@"default.png"]; 
} 

default.png是默認的png PIC組合中的資源

+0

我做的像編輯代碼直接在PicDisplayViewController,但它仍然不顯示圖像 – user2240329

+0

請確保:你的'filePath'中是否有一個PNG? 'fileExists'是'YES'或'NO'? –

+0

它顯示沒有文件,但我正在保存文件 – user2240329

相關問題