ここで指定されたコードを使用して、画像を保存およびロードしました
あるView Controllerで一緒に使用すると正常に動作しますが、あるView ControllerでsaveImageメソッドを使用し、別のView Controllerで画像を読み込もうとすると、画像が空白になりました...
ビューコントローラー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;
}
}
そして、View ControllerでBと言って、..を使用して画像をロードしています。
- (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メソッドのView Controller Bで、次のことを行います
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
ImageView.image=[self loadImage];
ImageName.text = @"Cash Withdrawal";
}