我已经使用给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