我正在截屏并使用以下代码将它们写入文件:
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。
设置图像的正确方法是什么?
哇,它有点乱,希望它可读。