ios - 如何在 iOS 中切片图像和拉伸(stretch)

标签 ios objective-c uiimage slice

我需要拉伸(stretch)图像左右中心半圆保持原样。中间还需要半圆

enter image description here

我试过切片的概念,也试过下面的代码

UIImage *image = self.imgBGBottom.image;
CGFloat capWidth =  floorf(image.size.width / 2) - 50;
CGFloat capHeight =  0;
UIImage *capImage = [image resizableImageWithCapInsets:
                     UIEdgeInsetsMake(capHeight, capWidth, capHeight, capWidth)];

[self.imgBGBottom setImage:capImage];

但它对我不起作用

请帮帮我。提前致谢。

最佳答案

您正在使用函数,请使用 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight 而不是 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets.

如以下代码所述,设置左上角的 cap 以拉伸(stretch)图像。

objective-C

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

CGFloat capTop =  50;  // top cap to keep half round as is
CGFloat capLeft = 5; // To repeat it or stretch equally.
UIImage *capImage = [image stretchableImageWithLeftCapWidth:capLeft topCapHeight:capTop];

swift

let image =  UIImage(named: "stretchableImage")

let capTop:Int =  50;  // top cap to keep half round as is
let capLeft:Int = 5; // To repeat it or stretch equally.
let capImage = image?.stretchableImage(withLeftCapWidth: capLeft, topCapHeight: capTop)

替代网络解决方案

同样的结果也可以使用下面的函数来实现。

objective-C

UIImage *stretchedImage = [image resizableImageWithCapInsets:
                 UIEdgeInsetsMake(50, 50, 0, 50)];

swift

var stretchedImage = image?.resizableImage(withCapInsets: UIEdgeInsets(top: 50, left: 50, bottom: 0, right: 50), resizingMode: .stretch) 

Note : Keep stretchable image as small as possible otherwise it will not stretch properly with smaller image container(ImageView, Button etc.). You can reduce height & width of your existing image.

关于ios - 如何在 iOS 中切片图像和拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284749/

相关文章:

iphone - 更改导航 Controller 后退按钮的颜色

调用了 iOS didReceiveRemoteNotification,但未清除通知下拉列表

iphone - 自动与中央数据库同步

objective-c - 范围或初始化问题?

iphone - REST 套件 0.10.0 - POST 不工作

Objective-C:自动化

iphone - 加载 ico 图像会切换红色 channel 和蓝色 channel

iphone - 将照片保存到 iPhone 照片库中的自定义相册

ios - 类似查询的 whereWithFormat 的 DBAccess 格式结构

iOS - 通过 HTTP POST 上传图片