iphone - 如何使用 CGContextDrawTiledImage 平铺图像?

标签 iphone cocoa-touch

我希望能够像 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/

相关文章:

ios - 如何在 iOS 的多个 friend 墙上分享 google Plus 中的消息?

ios - Xamarin.Forms 和 iOS : how to combine UseSafeArea and a background image?

cocoa-touch - 是否可以使用 NSTimer 和 UIDatepicker 来发出警报?

ios - 如何正确继承 UITableViewController

iphone - 在 iphone 应用程序中的一个线程之后安排一个线程

iphone - 更改 cell.accessoryView 和 cell.editingAccessoryView 的背景颜色

使用 spritekit 的 iPad 和 iPhone 游戏的 iOS 坐标

objective-c - iOS 6 无法自动旋转

ios - 异步请求运行缓慢 - iOS

iphone - UITextView 中不必要的换行符