所以我想通了一个解决方案。这是我觉得有点笨拙,但它的工作原理。
基本上我所做的就是用指定的左右帽来创建我的可拉伸图像。然后我用它初始化一个UIImageView。然后我调整图像视图的框架到所需的宽度。这将适当调整其中包含的图像的大小。
然后最后我用了一段我发现的代码,通过垂直平铺现在包含在图像视图中的调整图像来创建一个新图像。请注意,如何访问原始UIImage而不是使用UIImageView视图的图像属性。
的最后一件事然后是设置新的UIImage作为视图
这里的背景图案颜色的代码:
// create a strechable image
UIImage *image = [[UIImage imageNamed:@"[email protected]"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 60, 0, 60)];
// create the image view that will contain it
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect frame = imageView.frame;
frame.size.width = self.view.bounds.size.width;
imageView.frame = frame;
// create the tiled image
CGSize imageViewSize = imageView.bounds.size;
UIGraphicsBeginImageContext(imageViewSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextDrawTiledImage(imageContext, (CGRect){ CGPointZero, imageViewSize }, imageView.image.CGImage);
UIImage *finishedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:finishedImage];
是的我有,这种方法的问题是,我会需要一个图像我会支持每个视图宽度。因此,在这种情况下,iPhone为320px,然后iPad为横向和纵向宽度。三听起来可能听起来不多,但我希望我的代码能够适应任何情况。基本上我想要将来证明它。 – gossainapps 2013-03-22 22:20:12
这里有一个权衡。像这样设置backgroundColor会导致[应用程序中出现泄漏](http://stackoverflow.com/questions/9703396/ios-high-memory-usage)。我还不确定是否有更好的选择,所以这更多的是让你知道。 – Maxwell 2014-12-17 16:22:27