我正在开发表情符号键盘。这是我的方法:
- 我决定使用 UICollectionView。我在代码中做所有事情,不打算使用 Xib 文件。
我创建了 UICollectionViewCell 的子类。这将包含一个显示表情符号的标签。这就是我在其 initWithFrame 中所做的
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { if (_label == nil) { _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; _label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); _label.textAlignment = NSTextAlignmentCenter; [_label setNumberOfLines:1]; self.contentView.layer.cornerRadius = 6.0; [self.contentView addSubview:_label]; } } return self; }
在 UICollectionView 数据源对象中,我读取了一个包含 NSDictionary 的 plist 文件,其中 NSString 作为键,NSArrays 作为值。在每个 NSArray 中,可以找到我要显示的表情符号。然后我将字典存储在一个属性中。这是代码:
@property (nonatomic, strong) NSDictionary *emojis; - (NSDictionary *)emojis { if (!_emojis) { NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"EmojisList" ofType:@"plist"]; _emojis = [NSDictionary dictionaryWithContentsOfFile:plistPath]; } return _emojis; }
在下面的方法中,我尝试填充单元格:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier = @"Cell"; EmojiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; cell.label.font = [UIFont systemFontOfSize:self.isiPad ? 47 : 33]; NSArray *dataArray = [self.emojis objectForKey:self.categoryNames[indexPath.section]]; cell.label.text = dataArray[indexPath.row]; return cell; }
我遇到的问题是,滚动时内存使用量会增加。这会导致在真实设备上崩溃。
请帮帮我。我测试了许多不同的方法来解决这个问题,但都没有成功。
这是仪器的屏幕截图。我真的不知道那些是关于什么的。
最佳答案
字体大小的作用。
“Apple Color Emoji”字体根据字体大小将表情符号字符替换为不同大小的 PNG 图像。较大的图像很快就会用完 40MB 的内存限制。
在我的例子中,我尝试使用 16 的字体大小并使用 1.5 比例变换使其足够大。结果看起来不太好,但至少它有效...
关于ios - 滚动 UICollectionView 使用大量内存导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29655552/