UIEdgeInsets is ignored in CGContextDrawImage in UIGraphicsBeginImageContextWithOptions

I'm struggling to get Image Insets to work when I draw to the clipboard without a screen.

Using resizableImageWithCapInsets: on UIImage directly setImage: the 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]; 

As shown below (on the left - raw scaling, on the right it scales using attachments):

Image stretched without adoImage stretched with decent insets

So, the correct option is that the top / botton / left / right lines are equally distributed. So far so good.

Now, if I try to do the same with the image that will be drawn, and then written to the buffer without a screen:

 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 lower (again on the left - raw scaling, on the right - inserts):

raw scaling, side bufferscaling with inset, side buffer (WRONG!) .

So it seems that the inserts are being ignored.

Are there any suggestions as to what is going on here? A fully functional example at http://people.apache.org/~dirkx/insetSample.zip and only key code at http://pastebin.com/rm8h6YFV .

Any suggestions are welcome.

Dw

+4
source share
1 answer

I would suggest that extensible UIImags are processed at a higher level than Quartz2D, so using CGContextDrawImage will not work.

Instead, you can use -[UIImage drawInRect] . This will lead to the current UIGraphics context, which is your raster context that you create in your code snippet.

Corresponding line:

 [base drawInRect:CGRectMake(0,0,self.bounds.size.width, self.bounds.size.height)]; 
+13
source

Source: https://habr.com/ru/post/1435882/


All Articles