我希望能够像 UIImageView 这样的 UI 对象并在其中平铺图像。
我已经能够使用 CGContextDrawTiledImage 来更新主窗口中的背景,但不能更新 ImageView 。如果我修改 MainView 类中的 drawRect 函数,我就可以做到这一点。
我觉得我已经很接近了,但仍然缺少一些东西。有人能指出我正确的方向吗?
- (void)drawRect:(CGRect)rect {
CGImageRef image = CGImageRetain(currentImage.CGImage);
CGRect imageRect;
imageRect.origin = CGPointMake(160, 240);
imageRect.size = CGSizeMake(320.0, 480.0);
CGContextRef uiContext = UIGraphicsGetCurrentContext();
CGContextClipToRect(uiContext, CGRectMake(0.0, 0.0, rect.size.width, rect.size.height));
CGContextDrawTiledImage(uiContext, imageRect, image);
}
最佳答案
如果您只想在 View 中平铺图像,请将其更改为普通 UIView 并使用 UIColor initWithPatternImage 平铺背景:
backgroundView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageWithContentsOfFile:[self bundlePath:@"some_tile_image.png" inDirectory:@"tiles"]]];
关于iphone - 如何使用 CGContextDrawTiledImage 平铺图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/679925/