【发布时间】:2015-01-24 19:14:23
【问题描述】:
我制作了一个应用,可以将设备的屏幕截图保存到应用的 Documents 目录中。
这个 UIImageView 然后读取保存的截图图像并显示它。到目前为止一切都很好。但是等一下,这在 iOS 8 上运行得非常好,但在 iOS 7 上它根本不显示任何东西,无论是模拟器还是设备。
于是我调查发现iOS 8 将图片保存到以下目录(OS X 上的模拟器):
/Users/SunburstEnzo/Library/Developer/CoreSimulator/Devices/AE8252CE-38C1-4D81-BA87-34F50E743AEC/data/Containers/Data/Application/34562F4C-5DED-4F1C-9CB6-92948B6C5F89/Documents/2014-11- 26-11-12-24.png
而 iOS 7 将其保存到此处:
/Users/SunburstEnzo/Library/Developer/CoreSimulator/Devices/CE8E7B90-F00F-441B-8E70-17E968AB7AF7/data/Applications/994CD541-2730-4E32-93CD-23B9F996C2E3/Documents/2014-11-26-11- 12-51.png
我知道这与它有关,但是当我引用它时,它可以很好地看到图像,只是将它放到屏幕上时出现问题。而且我知道我所做的工作(在 iOS 8 上)所以代码很好,但我需要解决这个错误。
另外,无论我的问题是什么,都应该类似于这个问题关于转换为使用 App Containers 的问题(注意:保存图像对我来说很好,只是显示它) -- saving image to documents directory no longer works iOS8
Tl;dr iOS 7 中的错误停止显示 Documents 目录中保存的图像,在 iOS 8 上运行良好,帮我解决它
我的代码:
将屏幕截图写入 Documents 目录: UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
//UIImageWriteToSavedPhotosAlbum(sourceImage,nil, nil, nil);
NSData *pngData = UIImagePNGRepresentation(sourceImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
NSString *dateString;
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
dateString = [dateFormatter stringFromDate:now];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",dateString]]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
阅读代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
directoryContentsPopover = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
NSLog(@"Number of images: %d",directoryContents.count);
if (directoryContentsPopover.count > 0) {
self.mainImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
NSLog(@"At least 1 image; Image location: %@/%@",documentsPath,directoryContentsPopover[0]);
}
****尼克·洛克伍德的回答****
替换
self.mainImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
与
self.mainImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
【问题讨论】:
-
从 Documents 目录写入/读取图像时提供部分代码
-
@arturdev interesni 事情
标签: ios objective-c iphone uiimageview