1、
有的时候我们需要将一张图片平铺在View上当做背景。
以实现滚动页面时让背景也跟随移动。(例如苹果iBook首页背景的书架)
其实实现起来很简单。
代码如下:
在进行ios开发时,我门有时候会为了节省安装包大小而采取用局部图片平铺作为背景图片。
UIColor *color = [[UIColor alloc] initWithPatternImage:bgImg];
UIView *bg = [[UIView alloc] initWithFrame:self.tableView.frame];
[bg setBackgroundColor:color];
[color release];
[self.tableView setBackgroundView:bg];
[bg release];
第一种做法,没有经过测试,只是参考网上的做法,第二种方式一般情况下不用创建新的UIView,而可以只是直接设置背景色,当设置背景色的做法不起作用的的时候,可以考虑像上如做法设置背景View
3、
UIImage *imgQuestionBgBottom =[UIImageimageNamed:@"profile_arrow.png"];
UIImage *imgQuestionBgMiddle =[UIImageimageNamed:@"profile_arrow1.png"];
float tileHeight = 8;
float tileWidth = 9;
float imageHeight = tileHeight*5;UIGraphicsBeginImageContext(CGSizeMake(tileWidth,
imageHeight+imgQuestionBgBottom.size.height));[imgQuestionBgMiddle drawAsPatternInRect:CGRectMake(0, 0,
tileWidth, imageHeight)];
[imgQuestionBgBottom drawInRect:CGRectMake(0, imageHeight,
imgQuestionBgBottom.size.width,imgQuestionBgBottom.size.height)];
UIImage* imgBg = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
self.leftTopImageView.image = imgBg;
self.leftTopImageView.frame = CGRectMake(0, 0,imgBg.size.width, imgBg.size.height);
实例:下面分别是imgQuestionBgMiddle,imgQuestionBgBottom组成imgBg。
xcdoc://ios/samplecode/QuartzDemo/Listings/QuartzDemo_QuartzImages_m.html
#import "QuartzImages.h" @implementation QuartzImageView { CGImageRef _image; } -(void)drawInContext:(CGContextRef)context { CGRect imageRect; imageRect.origin = CGPointMake(8.0, 8.0); imageRect.size = CGSizeMake(64.0, 64.0); // Note: The images are actually drawn upside down because Quartz image drawing expects // the coordinate system to have the origin in the lower-left corner, but a UIView // puts the origin in the upper-left corner. For the sake of brevity (and because // it likely would go unnoticed for the image used) this is not addressed here. // For the demonstration of PDF drawing however, it is addressed, as it would definately // be noticed, and one method of addressing it is shown there. // Draw the image in the upper left corner (0,0) with size 64x64 CGContextDrawImage(context, imageRect, self.image); // Tile the same image across the bottom of the view // CGContextDrawTiledImage() will fill the entire clipping area with the image, so to avoid // filling the entire view, we'll clip the view to the rect below. This rect extends // past the region of the view, but since the view's rectangle has already been applied as a clip // to our drawing area, it will be intersected with this rect to form the final clipping area CGContextClipToRect(context, CGRectMake(0.0, 80.0, self.bounds.size.width, self.bounds.size.height)); // The origin of the image rect works similarly to the phase parameter for SetLineDash and // SetPatternPhase and specifies where in the coordinate system the "first" image is drawn. // The size (previously set to 64x64) specifies the size the image is scaled to before being tiled. imageRect.origin = CGPointMake(32.0, 112.0); CGContextDrawTiledImage(context, imageRect, self.image); // Highlight the "first" image from the DrawTiledImage call. CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 0.5); CGContextFillRect(context, imageRect); // And stroke the clipped area CGContextSetLineWidth(context, 3.0); CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); CGContextStrokeRect(context, CGContextGetClipBoundingBox(context)); } - (CGImageRef)image { if (_image == NULL) { NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"png"]; UIImage *img = [UIImage imageWithContentsOfFile:imagePath]; _image = CGImageRetain(img.CGImage); } return _image; } -(void)dealloc { CGImageRelease(_image); }