拉伸图片(两边拉伸,中间不变形)

效果图

拉伸图片(两边拉伸,中间不变形)_第1张图片
效果图.png

上下拉伸
- (UIImage *)hcl_stretchTopAndDownWithContainerSize:(CGSize)imageViewSize
{
    CGSize imageSize           = self.size;
    CGSize bgSize              = CGSizeMake((int)(imageViewSize.width), (int)(imageViewSize.height));
    UIImage *image             = [self stretchableImageWithLeftCapWidth:(int)(imageSize.width * 0.8) topCapHeight:(int)(imageSize.height * 0.8)];
    CGFloat tempHeight         = (bgSize.height) / 2 + (imageSize.height) / 2;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(bgSize.width, tempHeight), NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0,bgSize.width, tempHeight)];
    UIImage *firstStrechImage  = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImage *finalStretchImage = [firstStrechImage stretchableImageWithLeftCapWidth:(int)(imageSize.width * 0.8) topCapHeight:(int)(imageSize.height * 0.2)];
    
    return finalStretchImage;
}

左右拉伸
- (UIImage *)hcl_stretchLeftAndRightWithContainerSize:(CGSize)imageViewSize
{
    CGSize imageSize           = self.size;
    CGSize bgSize              = CGSizeMake((int)(imageViewSize.width), (int)(imageViewSize.height));
    UIImage *image             = [self stretchableImageWithLeftCapWidth:(int)(imageSize.width * 0.8) topCapHeight:(int)(imageSize.height * 0.8)];
    CGFloat tempWidth          = (bgSize.width) / 2 + (imageSize.width) / 2;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(tempWidth, bgSize.height), NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0, tempWidth, bgSize.height)];
    UIImage *firstStrechImage  = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImage *finalStretchImage = [firstStrechImage stretchableImageWithLeftCapWidth:(int)(imageSize.width * 0.2) topCapHeight:(int)(imageSize.height * 0.8)];
    
    return finalStretchImage;
}

你可能感兴趣的:(拉伸图片(两边拉伸,中间不变形))