Iphone 将UIIView保存到图像,存储在磁盘上,然后为视网膜重新显示
我有一个相当复杂的工作流程,在视网膜显示器上遇到问题。我有一个scrollview,最多可以包含19个子视图。这些子视图中的每一个子视图都由几个不同的组件(几个UILabel、一个UIImageView和一些UIButton)组成。在任何给定的时间,屏幕上都有多个滚动视图,可能有数百个子视图。为了节省开销,我在内存中创建每个子视图,然后将其展平为UIImage,仍然在内存中。从那里,我使用UIImage作为子视图来显示 作为另一个节省成本的步骤,我决定在合成这些UIImages后将其保存到磁盘上,将路径存储在核心数据中,并将其从磁盘上取下,而不是一直重新创建它们。所以,首先我检查磁盘上是否有该文章的图像。若并没有,我创建它,保存到磁盘,并在屏幕上显示它 以下是我为实现这一目标而采取的步骤:Iphone 将UIIView保存到图像,存储在磁盘上,然后为视网膜重新显示,iphone,ios,uiimage,retina-display,Iphone,Ios,Uiimage,Retina Display,我有一个相当复杂的工作流程,在视网膜显示器上遇到问题。我有一个scrollview,最多可以包含19个子视图。这些子视图中的每一个子视图都由几个不同的组件(几个UILabel、一个UIImageView和一些UIButton)组成。在任何给定的时间,屏幕上都有多个滚动视图,可能有数百个子视图。为了节省开销,我在内存中创建每个子视图,然后将其展平为UIImage,仍然在内存中。从那里,我使用UIImage作为子视图来显示 作为另一个节省成本的步骤,我决定在合成这些UIImages后将其保存到磁盘上
// create ViewController (cut initialization code for brevity)
TileViewController *tile = [[TileViewController alloc] init];
// Figure out if working on Retina enabled device
static CGFloat scale = -1.0;
if(scale < 0.0){
UIScreen *screen = [UIScreen mainScreen];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
scale = [screen scale];
}else{
scale = 0.0;
}
}
if(scale>0.0){
UIGraphicsBeginImageContextWithOptions(tile.view.bounds.size, NO, scale);
}else{
UIGraphicsBeginImageContext(tile.view.bounds.size);
}
[tile.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image);
NSString *savePath = <create save path here>;
// Snipped out the code that saves this path to the core data store =
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:savePath
contents:imageData
attributes:nil];
问题是,当图像不是生成的,而是从文件中提取的时候,UILabels中的文本在视网膜显示器上显得非常模糊。我可以通过在模拟器中运行这个程序,并在mac上的finder中查看生成的图像来查看磁盘上的图像,它们看起来清晰,大小合适(2倍的比例)
我在这里还需要采取其他措施吗?我已经找到了解决办法。您必须设置CALayer的适当
光栅化比例属性:
tile.layer.rasterizationScale = [[UIScreen mainScreen] scale];
我在这方面找到了解决办法。您必须设置CALayer的适当光栅化比例属性:
tile.layer.rasterizationScale = [[UIScreen mainScreen] scale];