【发布时间】:2013-07-06 09:08:21
【问题描述】:
我已经使用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 我正在加载图像使用..
- (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 实例在 imageWithContentsOfFile 之后是否为零?
-
似乎问题在于加载后显示 UIImage 而不是加载数据本身。您可以显示您正在使用的代码以便在屏幕上显示它吗?
-
我已经更新了我的问题.. :)
标签: ios objective-c nsdocument