Skip to content

Instantly share code, notes, and snippets.

@PhilCai1993
Last active October 1, 2017 02:33
Show Gist options
  • Save PhilCai1993/c9d63e9e40b166d1a64f to your computer and use it in GitHub Desktop.
Save PhilCai1993/c9d63e9e40b166d1a64f to your computer and use it in GitHub Desktop.
CALayer and [UIImage resizableImageWithCapInsets:resizingMode]
static CGRect CGRectCenterRectForResizableImage(UIImage* image) {
return CGRectMake(image.capInsets.left / image.size.width,
image.capInsets.top / image.size.height,
(image.size.width - image.capInsets.right - image.capInsets.left) / image.size.width,
(image.size.height - image.capInsets.bottom - image.capInsets.top) / image.size.height);
}
...
CALayer *layer = [CALayer layer];
UIImage *originImage = [UIImage imageNamed:@"shopping_bg_reply"];
UIEdgeInsets imageInset = UIEdgeInsetsMake(10, 22, 23, 8);
UIImage *resource = [originImage resizableImageWithCapInsets:imageInset
resizingMode:UIImageResizingModeStretch];
self.contentsCenter = CGRectCenterRectForResizableImage(self.resourceImage);
self.contentsScale = [UIScreen mainScreen].scale;
self.contents = (id)self.resource.CGImage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment