将 UIIView 保存到图像,存储在磁盘上,然后在 Retina 上重新显示
我有一个相当复杂的工作流程,在视网膜显示屏上遇到问题。我有一个最多可以包含 19 个子视图的滚动视图。这些子视图中的每一个都由几个不同的组件(几个 UILabels、一个 UIImageView 和一些 UIButtons)组成。在任何给定时间,屏幕上都有多个滚动视图,总共可能有数百个子视图。为了节省开销,我在内存中创建每个子视图,然后将其展平为仍在内存中的 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];
从现在起,我在 UIImageView 中使用 UIImage,并将其放置在屏幕上。第一次运行时,当从内存中显示时,它看起来很棒。第二次,我使用以下代码从磁盘中提取相同的图像:
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:article.tileImagePath]) {
UIImage *image = [UIImage imageWithContentsOfFile:article.tileImagePath];
// send image to UIImageView
}
问题是,当未生成图像而是从文件中提取图像时,UILabels 中的文本在视网膜显示屏上显得非常模糊。我可以通过在模拟器中运行它并在 Mac 上的取景器中查看它们来查看磁盘上生成的图像,它们看起来清晰且大小正确(2 倍比例)。
我应该在这里采取额外的步骤吗?
I have a rather complicated workflow that is encountering issues on the retina display. I have a scrollview that can consist of up to 19 subviews. Each one of these subviews is composited of several different components (several UILabels, a UIImageView, and some UIButtons). There are multiple scrollviews on screen at any given time, with the potential of hundreds of total subviews. To save on overhead, I create each subview in memory, then flatten it to a UIImage, still in memory. From there I use the UIImage as the subview to display.
As another cost saving step, I decided to save these UIImages out to disk after they were composited, store the path in core data, and pull them off of disk in lieu of recreating them all the time. So, first I check on disk to see if an image for that article is present. If not, I create it, save it to disk, and display it on screen.
Here are the steps I am taking to accomplish this:
// 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];
From this point, I use the UIImage in an UIImageView and it gets placed on screen. On first run, when being displayed from memory, it looks great. The second time around, I am pulling the same image from disk using the following code:
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:article.tileImagePath]) {
UIImage *image = [UIImage imageWithContentsOfFile:article.tileImagePath];
// send image to UIImageView
}
The problem is that on the times when the image is not generated, but is pulled from file, the text in the UILabels appears very blurry on the retina display. I can look at the generated images on disk by running this in the simulator and viewing them in the finder on the mac, and they appear crisp and are properly sized (2 times scale).
Is there an additional step I should be taking here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这个问题中找到了解决方案。您必须设置 CALayer 的正确
rasterizationScale
属性:I've found a solution in this question. You have to set the proper
rasterizationScale
property of your CALayer: