【问题标题】:Setting UIImageView image from saved screenshot working in iOS 8 but not iOS 7从保存的屏幕截图中设置 UIImageView 图像在 iOS 8 但不是 iOS 7
【发布时间】: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


【解决方案1】:

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

请改用 [UIImage imageWithContentsOfFile:]。

【讨论】:

  • 干得好,这行得通,我知道这是小事。非常感谢尼克 :) 当 SO 允许我时,我会将其标记为答案
【解决方案2】:

如果图像保存在 ios8 上,那么它也被保存在 ios7 中, 你没有得到正确的文档目录路径 ios8 和 ios7 都有不同的文档目录路径

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

试试这个

【讨论】:

    猜你喜欢
    • 2014-02-06
    • 2018-09-16
    • 1970-01-01
    • 2023-03-19
    • 2015-01-04
    • 2014-07-02
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多