我通过使用以下代码保存图像-
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];
NSString *filePath= [NSString stringWithFormat:@"%@/%@.jpg", DOCUMENTS_FOLDER,caldate];
imageurl = [NSURL fileURLWithPath:filePath];
dataImage = UIImagePNGRepresentation(myimage);
[dataImage writeToFile:filePath atomically:YES];
imageurl的打印如下
文件://localhost/Users/varmab/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/E2EDF729-4684-465B-8BE3-2C0ACFA2EC03/Documents/2013-03-18%2012:02:31%20 + 0000.jpg
现在,我要显示保存的图像,即图像存储在上述文件路径中
我尝试了以下代码:
//1st process:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];
//2nd process:
NSURL *urlString =imageurl;
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:urlString]])];
UIImage *image = [UIImage imageWithData:imageData];
//3rd process:
NSData * imageData = [[NSData alloc] initWithContentsOfURL:imageurl ];
self.testingImageView.image = [UIImage imageWithData: imageData];
请指导我,我有什么错误吗?
最佳答案
您为什么不直接使用此方法:
- (id)initWithContentsOfFile:(NSString *)path
它已经在
UIImage
类中可用。[self.testingImageView setImage: [[UIImage alloc] initWithContentsOfURL:imageurl]];
试试这个,让我知道您是否再次遇到相同的问题。
注意:检查图片网址是否与保存该图片时获得的旧网址相似。
关于ios - 如何获取本地保存的UIImage到UIImage的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15478394/