我正在使用此代码截取屏幕并将其写入文件:
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]];
dataFilePath方法:
- (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。
设置图像的正确方法是什么?
哇,它变得凌乱,希望它的可读性。答案 0 :(得分:1)
乍一看,假设您的读/写代码正确,您将使用PNG表示保存图像。 因此,在阅读时,您需要创建一个像这样的UIImage:
UIImage * myPic = [UIImage imageWithData:[pics objectAtIndex:row]];
然后尝试显示该图像