本文实例为大家分享了ios图片拉伸的具体代码,供大家参考,具体内容如下
1. uiimageview整体拉伸
uiimageview-contentmode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
typedef ns_enum(nsinteger, uiviewcontentmode) {
uiviewcontentmodescaletofill, // 默认 拉伸(会变形)
uiviewcontentmodescaleaspectfit, // 等比例拉伸
uiviewcontentmodescaleaspectfill, // 等比例填充
uiviewcontentmoderedraw, // redraw on bounds change (这个不清楚)
uiviewcontentmodecenter, // 下面的就是不拉伸按位置显示了
uiviewcontentmodetop,
uiviewcontentmodebottom,
uiviewcontentmodeleft,
uiviewcontentmoderight,
uiviewcontentmodetopleft,
uiviewcontentmodetopright,
uiviewcontentmodebottomleft,
uiviewcontentmodebottomright,
};
|
demo:https://github.com/vitoziv/vicmaimageview
2. uiimage局部拉伸
1
2
3
4
5
6
7
8
9
10
|
// 按4边间距显示不拉伸的区域
- (uiimage *)resizableimagewithcapinsets:(uiedgeinsets)capinsets ns_available_ios(5_0);
// 按2点拉伸
- (uiimage *)stretchableimagewithleftcapwidth:(nsinteger)leftcapwidth topcapheight:(nsinteger)topcapheight;
- (uiimage *)resizableimagewithcapinsets:(uiedgeinsets)capinsets resizingmode:(uiimageresizingmode)resizingmode;
// 拉伸模式
typedef ns_enum(nsinteger, uiimageresizingmode) {
uiimageresizingmodetile, //进行区域复制模式拉伸
uiimageresizingmodestretch, //进行渐变复制模式拉伸
};
|
3.uiimage修改大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
//内缩放,一条变等于最长边,另外一条小于等于最长边
- (uiimage *)scaletosize:(cgsize)newsize {
cgfloat width = self.size.width;
cgfloat height= self.size.height;
cgfloat newsizewidth = newsize.width;
cgfloat newsizeheight= newsize.height;
if (width <= newsizewidth &&
height <= newsizeheight) {
return self;
}
if (width == 0 || height == 0 || newsizeheight == 0 || newsizewidth == 0) {
return nil;
}
cgsize size;
if (width / height > newsizewidth / newsizeheight) {
size = cgsizemake(newsizewidth, newsizewidth * height / width);
} else {
size = cgsizemake(newsizeheight * width / height, newsizeheight);
}
return [self drawimagewithsize:size];
}
- (uiimage *)drawimagewithsize: (cgsize)size {
cgsize drawsize = cgsizemake( floor (size.width), floor (size.height));
uigraphicsbeginimagecontext(drawsize);
[self drawinrect:cgrectmake(0, 0, drawsize.width, drawsize.height)];
uiimage *newimage = uigraphicsgetimagefromcurrentimagecontext();
uigraphicsendimagecontext();
return newimage;
}
|
4.images.xcassets
多亏了xcode中asset catalog的slice和dice,我们不需要代码也能拉伸图片。首先在xcode中选中图片,然后点击右下角的show slicing:
你现在应该能看到slicing 面板和一个按钮"start slicing"。
在你点击按钮之后,会显示下面的三个选项:
左边的按钮用于horizontal edge insets,右边的按钮用于vertical edge insets,中间的则是两个都有。在我们的例子中要保留圆角,所以我们按中间的按钮,告诉系统我们想要按钮的中间在水平和垂直方向拉伸。在按下按钮之后,就能看到一些可以拖动的细条,这可以设置从哪里开始拉伸图片。
系统会保留深紫色的区域,浅紫色的区域会被拉伸。
更厉害的是,xcode自动找到了圆角,所以我们不需要设置从哪里开始拉伸图片。最后别忘了在attribtues pane中设置图片是可拉伸的。
如果我是你的话,我就会尝试并习惯这个功能。有了这个无价之宝,你就不用再在resizableimagewithcapinsets方法中填写那些神奇的数字了,也能帮助你分离view逻辑和app逻辑。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
正则表达式解决input框固定输入值得格式(金额,特殊字符)
2025-05-29 95 -
2025-06-04 73
-
2025-06-04 52
-
2025-05-29 27
-
2025-05-25 15