我有 3 张图像,左帽、右帽和中心。也许使用 CGImageRefs (?),有没有一种方法可以基于 CGRect 构建位图,该位图会将中心部分放在某个容器中,拉伸(stretch)左盖直到它到达中心部分,右边也一样,然后生成 UIImage

最佳答案

好的,我按照 promise 查找了它。这是一个解决方案:

基本理念

  • 取左帽并制作一个带有拉伸(stretch)右侧的 UIImage
  • 用箭头“粘合”它 UIImage
  • ,现在用右盖将其粘上,左侧拉伸(stretch)侧

  • 这是一个小图形,我认为它是如何表达的
      ______ ........    ________      .........___________
     {______|........|  | arrow  |    |.........|__________)
    left cap|lc flex     --------      rc flex  | right cap
    

    编码
    // get the images from disk
    UIImage *leftCap = [UIImage imageNamed:@"leftCap.png"];
    UIImage *arrow = [UIImage imageNamed:@"arrow.png"];
    UIImage *rightCap = [UIImage imageNamed:@"rightCap"];
    
    // stretch left and right cap
    // calculate the edge insets beforehand !
    
    UIImage *leftCapStretched = [leftCap resizableImageWithCapInsets:UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset)];
    UIImage *rightCapStretched = [rightCap resizableImageWithCapInsets:UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset)];
    
    CGFloat widthOfAllImages = leftCapStretched.size.width + arrow.size.width + rightCapStretched.size.width;
    
    // build the actual glued image
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthOfAllImages, arrow.size.height), NO, 0);
    [leftCapStretched drawAtPoint:CGPointMake(0, 0)];
    [arrow drawAtPoint:CGPointMake(leftCap.size.width, 0)];
    [rightCapStretched drawAtPoint:CGPointMake(leftCap.size.width + arrow.size.width, 0)];
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // now you should have a ready to use UIImage ;)
    

    替代方案

    受 Jonathan Plaketts 的启发,仅使用 UIImageViews 和拉伸(stretch) UIImages 就可以回答同样的问题。

    关于objective-c - 使用多个可拉伸(stretch)图像构建 UIImage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12002908/

    10-09 07:31
    查看更多