objective-c - 带有 resizableImage 选项的 UIImage

标签 objective-c ios uiimage

我想将图像设置为按钮的背景。所以我创建了一个图像:

Button

然后我想将它实现为可拉伸(stretch)的:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200, 400)];  
[button setTitle:@"Test Button" forState:UIControlStateNormal];

UIImage *buttonImage = [_model.background resizableImageWithCapInsets:UIEdgeInsetsMake(6, 8, 6, 7)]; //this is screen 1
//UIImage *buttonImage = _model.background; //this is screen 2   

[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];        
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self addSubview:button];

我想像在屏幕 2 中一样进行背景拉伸(stretch),但角落会像屏幕 1 一样锐利。_model.background 是 UIImage 包含来自这篇文章顶部的原始按钮背景。

Screen 1 Screen 2

我用对了吗?我遵循文档,所以 UIEdgeInsetsMake(top,left,bottom,right)应该是无法触及的角落吧?

最佳答案

根据文档,插图内的部分(未通过帽设置锁定的部分)是平铺的。所以你应该有一个小的 1 像素区域来平铺。例如。使插图为 UIEdgeIndetsMake(20,8,19,7)
这会产生:

enter image description here

关于objective-c - 带有 resizableImage 选项的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11616403/

相关文章:

ios - 从 CGPoints 数组创建 CGContext

ios - 使用 UIImageView 设置缩放 UIScrollView

ios - 用于在图像上叠加文本和图形的图形库和函数

ios - 使用来自另一个类的 (id)sender 调用函数不起作用

iphone - 编写与 NSPredicate 等效的 SQL - 初学者

iphone - SecCertificateRef : How to get the certificate information?

ios - 使用 AFNetworking 2.0 上传图像

ios - 在解析iOS中搜索1000多个查询

iphone - 为通用应用程序压缩图像

ios - 为 UITableView 的每一行推送不同的 UIView