iOS长宽不一致图片,设置圆角并裁切多余部分

问题:

直接设置圆角的话,如果所选图片长宽不一致,会导致展示的图片畸形,不是我们所想要的圆角

复制代码
_avatar.layer.cornerRadius = 40;
_avatarIcon.layer.masksToBounds = YES;

解决方案:

如果想要圆角完整显示,在代码中加入

_avatarIcon.contentMode = UIViewContentModeScaleAspectFill;

UIViewContentModeScaleAspectFill表示按比例缩放并且填满view,意味着图片可能超出view,可能被裁减

复制代码
_avatarIcon.contentMode = UIViewContentModeScaleAspectFill;//添加此代码
//设置圆角
_avatar.layer.cornerRadius = 40;
_avatarIcon.layer.masksToBounds = YES;
相关推荐
tangweiguo030519871 小时前
iOS vs Android 开发对照手册
android·ios
草帽lufei2 小时前
macOS中的Cursor等软件突然不能用了
macos·cursor
weixin_443478512 小时前
flutter组件学习之Cupertino 组件(iOS风格)
学习·flutter·ios
Rabbit_QL2 小时前
【Warp+Claude】任务完成自动通知(macOS + Warp 版)
macos·策略模式
X档案库4 小时前
【ROS2】Mac M4 虚拟机 Ubuntu 24.04 安装 ROS2 Jazzy 新手教程
macos·机器人·ros
江城月下5 小时前
从零开始:我在 Mac M1 上搭建离线 AI 知识库的实战记录
人工智能·macos
Andy_GF19 小时前
iOS26 系统适配-直接隐藏 UIBarButtonItem 的 Liquid glass 效果
ios
wjm0410061 天前
ios学习路线-- swift基础2
学习·ios·swift
开心就好20251 天前
全面解析WhatsApp Web抓包:原理、工具与安全
后端·ios
程序喵大人1 天前
OpenClaw的成功是AI Agent的“iPhone时刻”吗?
人工智能·ios·iphone·openclaw