IOS: Using A Pattern Image As A Background -Memory Leak

前端 未结 3 1544
没有蜡笔的小新
没有蜡笔的小新 2021-01-15 15:20

OK. I\'ll be looking for the answer, and may find it myself. I have a nasty habit of answering my own questions.

In any case, I have an app that is designed to be \"

3条回答
  •  暖寄归人
    2021-01-15 15:52

    UIColor colorWithPatternImage is buggy, do not use it. My experience is that it tends to greatly cripple performance on the device but not in the simulator. Anything like scrolling or animation on top of it tends to get slow. I'm not sure whether this really qualifies as a leak, I'm not seeing App being killed because RAM ran out. But if you profile the app, you will see that the app runs much slower with UIColor colorWithPatternImage enabled and drawing something.

    Eventually I created a subclass of UIView, and did something like this:

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetBlendMode(c, kCGBlendModeCopy);
        CGContextDrawTiledImage(c, CGRectMake(0, 0, bkgnd.size.width, bkgnd.size.height), bkgnd.CGImage);
    }
    

    This will tile the image. I then either use self.tableView.backgroundView or [self.view insertSubview:bkgnd atIndex:0] to make it a background. It runs much faster on the device, and causes zero memory leaks.

提交回复
热议问题