当前位置:  开发笔记 > 编程语言 > 正文

如何使用CGContextDrawTiledImage平铺图像?

如何解决《如何使用CGContextDrawTiledImage平铺图像?》经验,为你挑选了2个好方法。

我希望能够像UIImageView这样的UI对象并在其中平铺图像.

我已经能够使用CGContextDrawTiledImage更新主窗口中的背景,但不能更新图像视图.如果我修改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);

}



1> Chris Blackw..:

如果您只想在视图中平铺图像,请将其更改为普通UIView并使用UIColor initWithPatternImage平铺背景:

backgroundView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageWithContentsOfFile:[self bundlePath:@"some_tile_image.png" inDirectory:@"tiles"]]];


那会泄漏.使用这个`backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"some_tile_image.png"]];
我会在这里使用 - [UIImage imageNamed:],但是同样的想法.我认为也可以改变偏移量,但不要完全回忆起来.

2> Alex..:

假设你有一个CGImageRef想要平铺的图像tileImage和一个UIImageView被调用的图像imageView.你需要做的是创建一个UIImage分配给image属性的imageView.你可以这样做:

CGSize imageViewSize = imageView.bounds.size;
UIGraphicsBeginImageContext(imageViewSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextDrawTiledImage(imageContext, (CGRect){ CGPointZero, imageViewSize }, tileImage);
UIImage *finishedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

imageView.image = finishedImage;

这将创建所需大小的位图图像上下文,将图像平铺到该上下文中,从上下文中获取UIImage,然后将其分配给UIImageView.只要图像视图和图块图像已加载并准备就绪,您就可以将此代码放在任何位置.

推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有