在UIGraphicsBeginImageContextWithOptions中的CGContextDrawImage上忽略了UIEdgeInsets [英] UIEdgeInsets ignored on CGContextDrawImage within a UIGraphicsBeginImageContextWithOptions
问题描述
当绘制到屏幕外缓冲区时,我正在努力让Image Insets工作。
I am struggling getting Image Insets to work when drawing to an off screen buffer.
在UIImage上使用resizableImageWithCapInsets:直接setImage:进入按钮工作正常我:
Using the resizableImageWithCapInsets: on an UIImage directly setImage: into a button works fine for me:
UIImage * base = [UIImage imageNamed:@"button.png"];
UIImage * img = [base resizableImageWithCapInsets:UIEdgeInsetsMake(20,20,20,20)];
[self setImage:img forState:UIControlStateNormal];
如下所示(左边是原始缩放,右边是使用insets缩放):
As shown below (left is raw scaling, right is scaled with insets):
所以正确的一个很好 - 线条顶部/ botton / left / right等间距。到目前为止一切都那么好。
So the right one is fine - the lines top/botton/left/right are equally spaced. So far so good.
现在,如果我尝试使用绘制的图像进行相同的操作,然后使用以下内容捕获到屏幕外缓冲区:
Now if I try the very same thing with an image that is drawn and then captured to an off screen buffer with:
UIImage * base = [UIImage imageNamed:@"button.png"];
base = [base resizableImageWithCapInsets:UIEdgeInsetsMake(20,20,20,20)];
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
ctx = UIGraphicsGetCurrentContext();
CGContextDrawImage(ctx, CGRectMake(0,0,self.bounds.size.width,
self.bounds.size.height), [base CGImage]);
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setImage:img forState:UIControlStateNormal];
我得到以下(再次左边是原始缩放,右边是插图):
I get below (again left is raw scaling, right is with insets):
。
所以这里似乎是插图被忽略了。
So here it seems that the insets are ignored.
有人对这里出了什么问题有任何建议吗? http://people.apache.org/~dirkx/insetSample.zip <的完整功能示例/ a>以及 http://pastebin.com/rm8h6YFV 上的密钥代码。
Anyone any suggestions as to what is going wrong here ? Fully functional example at http://people.apache.org/~dirkx/insetSample.zip and just the key code at http://pastebin.com/rm8h6YFV.
赞赏任何和所有建议。
Dw
推荐答案
我猜可伸缩的UIImages的处理级别高于Quartz2D,所以使用 CGContextDrawImage
不会起作用。
I would guess that stretchable UIImages are handled at a higher level than Quartz2D and so using CGContextDrawImage
is not going to work.
您可以尝试使用 - [UIImage drawInRect]
。这将绘制到当前的UIGraphics上下文,这是您在代码段中创建的位图上下文。
You could try using -[UIImage drawInRect]
instead. This will draw to the current UIGraphics context, which is your bitmap context that you create in your code snippet.
相关行是:
[base drawInRect:CGRectMake(0,0,self.bounds.size.width,
self.bounds.size.height)];
这篇关于在UIGraphicsBeginImageContextWithOptions中的CGContextDrawImage上忽略了UIEdgeInsets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!