【问题标题】:My app uses a lot of memory when I use imageView.layer.shouldRasterize = YES当我使用 imageView.layer.shouldRasterize = YES 时,我的应用程序使用了大量内存
【发布时间】:2016-08-31 09:24:04
【问题描述】:

我的应用程序有一个 UITableView 和包含几个 UIImageViews 的单元格,这些 imageViews 有阴影层和cornerRadius。 为了提高性能,我使用

//if set to NO, tableView is slow to move
imageView.layer.shouldRasterize = YES;

//solve image become blurry problem
imageView.layer.rasterizationScale = [[UIScreen mainScreen] scale];

但是当我加载许多单元格时,它会使用大量内存并因以下错误而崩溃:

CGBitmapContextInfoCreate: 无法为位图数据分配 283136 字节

我该如何解决这个问题?

编辑 1

在 CAlayer.h 中

/* When true, the layer is rendered as a bitmap in its local coordinate
* space ("rasterized"), then the bitmap is composited into the
* destination (with the minificationFilter and magnificationFilter
* properties of the layer applied if the bitmap needs scaling).
* Rasterization occurs after the layer's filters and shadow effects
* are applied, but before the opacity modulation. As an implementation
* detail the rendering engine may attempt to cache and reuse the
* bitmap from one frame to the next. (Whether it does or not will have
* no affect on the rendered output.)
*
* When false the layer is composited directly into the destination
* whenever possible (however, certain features of the compositing
* model may force rasterization, e.g. adding filters).
*
* Defaults to NO. Animatable. */

@property BOOL shouldRasterize;

'尝试缓存'

我认为它将图像和这些阴影数据加载到内存中并导致它使用大量内存。

Performance scrolling and retina resolution images in CALayer

Drawing mask influence performance

完整代码:

imageView.layer.borderWidth = 0.2;
imageView.layer.borderColor = [UIColor colorWithWhite:0.95 alpha:1].CGColor;
imageView.layer.cornerRadius = 2;

imageView.layer.shouldRasterize = YES;
imageView.layer.rasterizationScale = [[UIScreen mainScreen] scale];


UIView *shadowView = [[UIView alloc] init];
shadowView.backgroundColor = [UIColor whiteColor];

//set layer, if I don't use shadow view
shadowView.layer.shadowColor = [UIColor colorWithWhite:0.93 alpha:1].CGColor;
shadowView.layer.cornerRadius = 2;
shadowView.layer.shadowOffset = CGSizeMake(1,1);
shadowView.layer.shadowOpacity = 1;
shadowView.layer.shadowRadius = 0.6;
shadowView.layer.shouldRasterize = YES;
shadowView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
[shadowView setFrame:containerFrame];

[shadowView addSubview:imageView];

//add shadowView to cell ...

【问题讨论】:

  • 嗯? 我怎样才能拥有鱼和熊掌?
  • @Avi,他的意思是他怎么能两者兼得。表格视图滚动完美并解决了这个内存崩溃。自有鱼熊掌(孟子成语)
  • 我以前从未听说过这个成语。也许这不是表达 SO 问题的最佳方式。

标签: ios objective-c uitableview core-graphics


【解决方案1】:

尝试在您的 imageView 下添加视图仅用于阴影并对其使用相同的设置,但不适用于 imageView。

确保 shadowView.layer.shouldRasterize = YES; shadowView.layer.rasterizationScale = [[UIScreen mainScreen] scale]; 在单元初始化时调用一次

【讨论】:

  • 其实我按照你说的做了。
猜你喜欢
  • 1970-01-01
  • 2012-07-08
  • 2020-02-07
  • 2013-09-22
  • 2016-06-30
  • 2016-03-07
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多