[英]Can't Reload UIImage from Saved file in iPhone Documents Directory location
我正在制作一個將文件保存到iPhones文檔目錄的應用程序,然后在重新加載View Controller時,它將從同一位置獲取文件並顯示它。 但是它不是獲取並顯示它。
我正在使用以下方法保存圖像:
void UIImageWriteToFile(UIImage *image, NSString *fileName)
{
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
}
我正在嘗試使用以下代碼調用從viewDidLoad方法保存的圖像:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *fileName = [NSString stringWithFormat:@"/%@.png", self.fullname.text];
NSString *filePath = [documentDirectoryPath stringByAppendingString:fileName];
[self.imageView setImage:[UIImage imageNamed: filePath]];
但是,它沒有顯示我的圖像。 誰能幫助我,告訴我我要去哪里了。 我是iOS開發的新手。
替換此行: [self.imageView setImage:[UIImage imageNamed: filePath]];
與這個:
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
[self.imageView setImage:image];
您可以將[UIImage imageNamed:]
與Documents文件夾中的圖像一起使用,並具有底層緩存機制的優點,但是文件路徑略有不同。
使用以下內容:
NSString *fileName = [NSString stringWithFormat:@"../Documents/%@.png", self.fullname.text];
UIImage *image = [UIImage imageNamed:fileName];
您需要使用方法
- (UIImage*)initWithContentsOfFile:(NSString*)path
從文檔或緩存目錄初始化圖像。
imageNamed:用於從應用程序捆綁包中獲取圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.