Stretching an UIImage while preserving the corners

前端 未结 5 1187
太阳男子
太阳男子 2020-12-12 20:55

I\'m trying to stretch a navigation arrow image while preserving the edges so that the middle stretches and the ends are fixed.

Here is the image that I\'m trying to

5条回答
  •  旧时难觅i
    2020-12-12 21:50

    You can extend UIImage to allow stretching an image with custom edge protection (thereby stretching the interior of the image, instead of tiling it):

    UIImage+utils.h:

    #import 
    @interface UIImage(util_extensions)
    //extract a portion of an UIImage instance 
    -(UIImage *) cutout: (CGRect) coords;
    //create a stretchable rendition of an UIImage instance, protecting edges as specified in cornerCaps
    -(UIImage *) stretchImageWithCapInsets: (UIEdgeInsets) cornerCaps toSize: (CGSize) size;
    @end
    

    UIImage+utils.m:

    #import "UIImage+utils.h"
    @implementation UIImage(util_extensions)
    -(UIImage *) cutout: (CGRect) coords {
        UIGraphicsBeginImageContext(coords.size);
        [self drawAtPoint: CGPointMake(-coords.origin.x, -coords.origin.y)];
        UIImage *rslt = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return rslt;
    }
    
    -(UIImage *) stretchImageWithCapInsets: (UIEdgeInsets) cornerCaps toSize: (CGSize) size { 
        UIGraphicsBeginImageContext(size);
    
        [[self cutout: CGRectMake(0,0,cornerCaps.left,cornerCaps.top)] drawAtPoint: CGPointMake(0,0)]; //topleft
        [[self cutout: CGRectMake(self.size.width-cornerCaps.right,0,cornerCaps.right,cornerCaps.top)] drawAtPoint: CGPointMake(size.width-cornerCaps.right,0)]; //topright
        [[self cutout: CGRectMake(0,self.size.height-cornerCaps.bottom,cornerCaps.left,cornerCaps.bottom)] drawAtPoint: CGPointMake(0,size.height-cornerCaps.bottom)]; //bottomleft
        [[self cutout: CGRectMake(self.size.width-cornerCaps.right,self.size.height-cornerCaps.bottom,cornerCaps.right,cornerCaps.bottom)] drawAtPoint: CGPointMake(size.width-cornerCaps.right,size.height-cornerCaps.bottom)]; //bottomright
    
        [[self cutout: CGRectMake(cornerCaps.left,0,self.size.width-cornerCaps.left-cornerCaps.right,cornerCaps.top)]
     drawInRect: CGRectMake(cornerCaps.left,0,size.width-cornerCaps.left-cornerCaps.right,cornerCaps.top)]; //top
    
        [[self cutout: CGRectMake(0,cornerCaps.top,cornerCaps.left,self.size.height-cornerCaps.top-cornerCaps.bottom)]
     drawInRect: CGRectMake(0,cornerCaps.top,cornerCaps.left,size.height-cornerCaps.top-cornerCaps.bottom)]; //left
    
        [[self cutout: CGRectMake(cornerCaps.left,self.size.height-cornerCaps.bottom,self.size.width-cornerCaps.left-cornerCaps.right,cornerCaps.bottom)]
     drawInRect: CGRectMake(cornerCaps.left,size.height-cornerCaps.bottom,size.width-cornerCaps.left-cornerCaps.right,cornerCaps.bottom)]; //bottom
    
        [[self cutout: CGRectMake(self.size.width-cornerCaps.right,cornerCaps.top,cornerCaps.right,self.size.height-cornerCaps.top-cornerCaps.bottom)]
     drawInRect: CGRectMake(size.width-cornerCaps.right,cornerCaps.top,cornerCaps.right,size.height-cornerCaps.top-cornerCaps.bottom)]; //right
    
        [[self cutout: CGRectMake(cornerCaps.left,cornerCaps.top,self.size.width-cornerCaps.left-cornerCaps.right,self.size.height-cornerCaps.top-cornerCaps.bottom)]
     drawInRect: CGRectMake(cornerCaps.left,cornerCaps.top,size.width-cornerCaps.left-cornerCaps.right,size.height-cornerCaps.top-cornerCaps.bottom)]; //interior
    
        UIImage *rslt = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return [rslt resizableImageWithCapInsets: cornerCaps];
    }
    
    @end
    

提交回复
热议问题