1

我正在截屏并使用以下代码将它们写入文件:

 if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);

NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [directories objectAtIndex:0];
NSString *key = [documentsDirectory stringByAppendingPathComponent:@"screenshots.archive"];

NSMutableArray *storageArray = [NSMutableArray arrayWithContentsOfFile:key];

if(!storageArray)
    storageArray = [NSMutableArray arrayWithObject:data];
else
    [storageArray addObject:data];

[storageArray writeToFile:key atomically:YES];

当我再次加载图像时,我会这样做:

pics = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];

ScreenshotViewController *scv = [[ScreenshotViewController alloc]init];

[[self navigationController]pushViewController:scv animated:YES];
NSInteger row = indexPath.row;
[scv setImage:[pics objectAtIndex:row]];

数据文件路径方法:

- (NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"screenshots.archive"];
}

问题是 ScreenshotViewController 中的 UIImageView 保持空白。我已经测试了各种各样的东西,这与图像保存/加载到数组中的方式有​​关,但我不确定。我还通过立即将其放入 UIImageView 并将其添加到视图中测试了实际的屏幕截图部分是否正常工作。

注意:如果我改变这个

[scv setImage:[pics objectAtIndex:row]];

对此

[scv.myUIImageView setImage:[pics objectAtIndex:row]];

我得到这个异常:[__NSCFData _isResizable]:无法识别的选择器发送到实例 0x847a280。

设置图像的正确方法是什么?

哇,它有点乱,希望它可读。

4

1 回答 1

1

乍一看,假设您的读/写代码是正确的,您正在使用 PNG 表示形式保存图像。因此,在阅读时,您需要像这样创建一个 UIImage:

UIImage * myPic = [UIImage imageWithData:[pics objectAtIndex:row]];

然后尝试显示该图像

于 2013-10-02T21:41:42.723 回答