繁体   English   中英

从保存的屏幕截图中设置UIImageView图像,该截图可在iOS 8中使用,但不能在iOS 7中使用

[英]Setting UIImageView image from saved screenshot working in iOS 8 but not iOS 7

我制作了一个将设备屏幕截图保存到应用程序“文档”目录的应用程序。

然后,此UIImageView读取保存的屏幕快照图像并显示它。 到目前为止一切都很好。 但是请稍等一下,这在iOS 8上绝对可以正常工作,但是在iOS 7上(模拟器和设备上)完全不显示任何内容。

因此,我进行了调查,发现iOS 8将图像保存到以下目录(OS X上的Simulator):

/用户/ SunburstEnzo /库/开发商/ CoreSimulator /设备/ AE8252CE-38C1-4D81-BA87-34F50E743AEC /数据/容器/数据/应用/ 34562F4C-5DED-4F1C-9CB6-92948B6C5F89 /文档/ 2014-11-26-11 -12-24.png

iOS 7将其保存到此处:

/用户/ SunburstEnzo /库/开发商/ CoreSimulator /设备/ CE8E7B90-F00F-441B-8E70-17E968AB7AF7 /数据/应用/ 994CD541-2730-4E32-93CD-23B9F996C2E3 /文档/ 2014-11-26-11-12-51巴纽

我知道这与它有关,但是当我引用它时,它可以看到很好的图像,将它放到屏幕上只是一个问题。 而且我知道我所做的工作(在iOS 8上)可以正常工作,因此代码很好,但是我需要针对此错误的解决方法。

此外,无论我遇到什么问题,都应该与该问题有关使用App Containers的转换类似(注意:保存图像对我来说很好,只显示它)- 将图像保存到文档目录不再有效

iOS 7中的tl; dr bug阻止显示在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]]];

您正在尝试使用[UIImage imageNamed:]加载图像,但是此方法仅适用于应用程序捆绑包或XCAssets中的图像,它不接受任意文件路径。

请改用[UIImage imageWithContentsOfFile:]。

如果图像保存在ios8上,那么它也已保存在ios7中,您将无法正确设置文档目录的路径ios8和ios7的文档目录路径均不同

ios7文档目录的路径:/ Users / ********** / Library / Application Support / iPhone Simulator

尝试这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM