English 中文(简体)
UITableView repeating background with 2 images
原标题:

So I m trying to make a UITableView that has a repeating background. I want the background to scroll with the text. The catch is that the background is 2 images. There is a top image that is about 550px in height, and then a repeater image that s 2px in height. I want the top image to scroll with the table view and then eventually go off the top and then the segment image just repeats. This view actually sits below a UITableView in a view controller.

Here s my drawrect for the background. I call setNeedsDisplay when ever the tableview on top scrolls.

-(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];
}

It works but it is very chuggy on my 3G. Does anyone have any optimization tips, or another way of doing this?

Cheers

问题回答

Don t create a new image each time; cache your UIImage, then use CoreGraphics calls to reposition your CGContextRef to point to the right area, blit the image there, and move on. If you were to profile the code above, I imagine that CGImageCreateWithImageInRect() was taking up the vast majority of your cycles.

You should probably do this using contentOffset, rather than whatever _tableViewRect is. Also, looking at your code, while getting rid of the stuff above will help, I m not sure it ll be enough, since you ll be redrawing a LOT. That said, here s a snippet that may work:

//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);
 }

This should be a replacement for the middle if(...) portion of your code. This is not tested, so you may need to futz with it a bit.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签