我已經使用給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";
}
是對UIImage的實例零? – Stavash
不,它不是零 – Audi
似乎問題是顯示UIImage後加載而不是加載數據本身。您能否顯示您使用的代碼以便在屏幕上顯示? – Stavash