首页 > 代码库 > image图片拉伸

image图片拉伸

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1d9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px } span.s1 { font: 18.0px Menlo } span.s2 { } span.s3 { color: #c32275 } span.s4 { color: #000000 } span.s5 { color: #6122ae } span.s6 { color: #3d1d81 } span.s7 { color: #0435ff } span.s8 { color: #c91b13 } span.s9 { font: 18.0px "PingFang SC"; color: #c91b13 } span.s10 { color: #703daa } span.s11 { font: 18.0px "PingFang SC" } span.s12 { font: 18.0px Menlo; color: #000000 }</style>

//iOS5以前设置拉伸图片

-(void)loadImageBeforeIOS5{

    //35*30

    UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(80, 180, 100, 40)];

    [nextButton setTitle:@"iOS5之前" forState:UIControlStateNormal];

    nextButton.layer.borderColor=[[UIColor redColor] CGColor];

    nextButton.layer.borderWidth=1.0f;

    UIImage *image = [UIImage imageNamed:@"Question"];

    // 设置左边端盖宽度  rightCap=width - leftCapWidth - 1

    NSInteger leftCapWidth = image.size.width * 0.5;

    // 设置上边端盖高度  bottom=height - topCapWidth - 1

    NSInteger topCapHeight = image.size.height * 0.5;

    UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];

    [nextButton setBackgroundImage:newImage forState:UIControlStateNormal];

    [self.view addSubview:nextButton];

}

//iOS5 拉伸图片

-(void)loadImageIOS5{

     UIImage *image = [UIImage imageNamed:@"Question"];

    UIButton  *resizableButton=[[UIButton alloc]initWithFrame:CGRectMake(80, 250, 100, 40)];

    [resizableButton setTitle:@"iOS5" forState:UIControlStateNormal];

    // 设置端盖的值

    CGFloat top = image.size.height * 0.5;

    CGFloat left = image.size.width * 0.5;

    CGFloat bottom = image.size.height * 0.5;

    CGFloat right = image.size.width * 0.5;

    

    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);

    

    // 拉伸图片

    UIImage *edgeImage = [image resizableImageWithCapInsets:edgeInsets];

    

    // 设置按钮的背景图片

    [resizableButton setBackgroundImage:edgeImage forState:UIControlStateNormal];

    [self.view addSubview:resizableButton];

}

//iOS6 拉伸图片

-(void)loadImageAfterIOS6{

    UIImage *image = [UIImage imageNamed:@"Question"];

    UIButton  *resizableButtonMode=[[UIButton alloc]initWithFrame:CGRectMake(80, 320, 180, 400)];

    [resizableButtonMode setTitle:@"iOS6" forState:UIControlStateNormal];

    [resizableButtonMode addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

    // 设置上左下右边距

    CGFloat topMode= image.size.height * 0.5;

    CGFloat leftMode= image.size.width * 0.5;

    CGFloat bottomMode= image.size.height * 0.5;

    CGFloat rightMode= image.size.width * 0.5;

    

    UIEdgeInsets edgeInsetsMode= UIEdgeInsetsMake(topMode, leftMode, bottomMode, rightMode);

    

    // 拉伸图片

    UIImage *edgeModeImage = [image resizableImageWithCapInsets:edgeInsetsMode resizingMode:UIImageResizingModeStretch];

//    UIImage *edgeModeImage = [image resizableImageWithCapInsets:edgeInsetsMode resizingMode:UIImageResizingModeTile];

    

    //设置图片

    [resizableButtonMode setBackgroundImage:edgeModeImage forState:UIControlStateNormal];

    [self.view addSubview:resizableButtonMode];

}

image图片拉伸