2013-07-06 116 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循环获取一个图像 –