【问题标题】:UITableView repeating background with 2 imagesUITableView 重复背景与 2 个图像
【发布时间】:2009-11-20 01:04:26
【问题描述】:

所以我正在尝试制作一个具有重复背景的 UITableView。我希望背景与文本一起滚动。问题是背景是2张图片。有一个高度约为 550 像素的顶部图像,然后是一个高度为 2 像素的中继器图像。我希望顶部图像与表格视图一起滚动,然后最终离开顶部,然后片段图像只是重复。此视图实际上位于视图控制器中的 UITableView 下方。

这是我的背景绘制。每当顶部的表格视图滚动时,我都会调用 setNeedsDisplay。

-(void)drawRect:(CGRect) rect 
{

    CGRect topRect = CGRectMake(0, _tableViewRect.origin.y, 320, min( max(0, CGImageGetHeight(top) - _tableViewRect.origin.y), _tableViewRect.size.height));
    CGRect segmentRect = CGRectMake(0, topRect.size.height, 320, _tableViewRect.size.height - topRect.size.height);

    if(topRect.size.height > 0)
    {
        CGImageRef newImageRef = CGImageCreateWithImageInRect(top, topRect);  
        UIImage *newImage = [UIImage imageWithCGImage:newImageRef];  
        [newImage drawAtPoint:CGPointMake(0,0)];
    }

    [[UIImage imageWithCGImage: segment] drawAsPatternInRect:segmentRect];
}

它可以工作,但在我的 3G 上非常笨拙。有没有人有任何优化技巧或其他方法?

干杯

【问题讨论】:

    标签: iphone objective-c uitableview background


    【解决方案1】:

    不要每次都创建新图像;缓存您的 UIImage,然后使用 CoreGraphics 调用将您的 CGContextRef 重新定位为“指向”正确的区域,在那里对图像进行 blit,然后继续。如果您要分析上面的代码,我想 CGImageCreateWithImageInRect() 占用了您的大部分周期。

    您可能应该使用 contentOffset 来执行此操作,而不是 _tableViewRect 是什么。此外,查看您的代码,虽然摆脱上面的东西会有所帮助,但我不确定这是否足够,因为您将重绘很多内容。也就是说,这是一个可能有效的 sn-p:

    //I'm not sure what _tableViewRect is, so let's just assume you've got
    //some variable, tableScrollY, that represents the scroll offset of your table
    
    CGContextRef      context = UIGraphicsGetCurrentContext();
    
    if (tableScrollY < top.size.height) {      //okay, we need to draw at least PART of our image
        CGContextSaveGState(context);
        CGContextDrawImage(context, CGRectMake(0, -tableScrollY, top.size.width, top.size.height), top.CGImage);
     }
    

    这应该是代码中间 if(...) 部分的替代品。这没有经过测试,因此您可能需要稍微尝试一下。

    【讨论】:

    • 哦,好吧。什么是最好的blit方法。这就是我想做的,但实际上什么都找不到。
    • 查看 CGContextRef 文档 (developer.apple.com/mac/library/DOCUMENTATION/GraphicsImaging/…),特别是 CGContextDrawImage。请记住,您可以使用 .CGImageRef 属性从 UIImage 中获取 CGImageRef。
    • 我无法弄清楚如何正确地进行 blit。你能告诉我任何讨论这个的文章吗?或者你有代码吗?非常感谢您的帮助。
    • 有什么方法可以用 CGContextDrawImage 关闭缩放功能?如果它不缩放图像,我会很高兴。
    • 你会想看看仿射变换,特别是 CGContextScaleCTM()。
    猜你喜欢
    • 2018-02-12
    • 2015-02-09
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2019-08-04
    • 1970-01-01
    相关资源
    最近更新 更多