How to let a UIImage only stretch in a specific area?

前端 未结 2 1104
甜味超标
甜味超标 2020-12-16 08:21
var contentImageView: UIImageView!{
    didSet{
        contentImageView.image!.resizableImageWithCapInsets(UIEdgeInsets(top: 5,left: 5,bottom: 5,right: 5))
    }
}
         


        
相关标签:
2条回答
  • 2020-12-16 08:51

    You can use the method

    - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
    

    capInsets is a specific area(e.g. UIEdgeInsetsMake(10, 18, 10, 18)), resizingMode should select UIImageResizingModeStretch .

    UIImage *image = [[UIImage imageNamed:@"bubble.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 18, 10, 18) resizingMode:UIImageResizingModeStretch];
    bubbleImageView.image = image;
    
    0 讨论(0)
  • 2020-12-16 09:00

    There is a sleek option in Xcode .xcassets. You can select the images and slice it from there so that it's strechable in specific parts only below are the images please explore this option.

    1. For your purpose just select the middle option (slice vertically and horizontally) for chat bubble.

    1. After Slicing.

    1. Top Image is image with slicing. Bottom image is image without slicing.

    Borrowed Bhavin's Image for demonstration :D

    0 讨论(0)
提交回复
热议问题