iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
发布时间 - 2026-01-11 00:25:46 点击率:次摘要:获得屏幕图像,label的动态size,时间戳转化为时间,RGB转化成颜色,加边框,压缩图片,textfield的placeholder,图片做灰度处理

1.获得屏幕图像
- (UIImage *)imageFromView: (UIView *) theView
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
2.label的动态size
- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
{
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
NSDictionary* attributes =@{NSFontAttributeName:[UIFont fontWithName:@"MicrosoftYaHei" size:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
labelSize.height=ceil(labelSize.height);
return labelSize;
}
3.时间戳转化为时间
-(NSString*)TimeTrasformWithDate:(NSString *)dateString
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YY-MM-dd HH:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
NSString *date = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:dateString.integerValue]];
//NSLog(@"date1:%@",date);
return date;
}
4.RGB转化成颜色
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString
{
UIColor *result = nil;
unsigned int colorCode = 0;
unsigned char redByte, greenByte, blueByte;
if (nil != inColorString)
{
NSScanner *scanner = [NSScanner scannerWithString:inColorString];
(void) [scanner scanHexInt:&colorCode]; // ignore error
}
redByte = (unsigned char) (colorCode >> 16);
greenByte = (unsigned char) (colorCode >> 8);
blueByte = (unsigned char) (colorCode); // masks off high bits
result = [UIColor
colorWithRed: (float)redByte / 0xff
green: (float)greenByte/ 0xff
blue: (float)blueByte / 0xff
alpha:1.0];
return result;
}
5.加边框
UIRectCorner corners=UIRectCornerTopLeft | UIRectCornerTopRight; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(4, 0)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
6.//压缩图片
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
//创建一个图形上下文形象
UIGraphicsBeginImageContext(newSize);
// 告诉旧图片画在这个新的环境,所需的
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//获取上下文的新形象
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
return newImage;
}
7.textfield的placeholder
[textF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; [textF setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
8.布局
butLeft. imageEdgeInsets = UIEdgeInsetsMake (7 , 5 , 7 , 25 ); butLeft.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
9.//调用此方法改变label最后2个字符的大小
- (void)label:(UILabel *)label BehindTextSize:(NSInteger)integer
{
NSMutableAttributedString *mutaString = [[NSMutableAttributedString alloc] initWithString:label.text];
[mutaString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(label.text.length-2, 2)];
label.attributedText = mutaString;
}
10.
- (void)ChangeLabelTextColor:(UILabel *)label
{
NSMutableAttributedString *mutaString = [[NSMutableAttributedString alloc] initWithString:label.text];
[mutaString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:207/255.0 green:34/255.0 blue:42/255.0 alpha:1] range:NSMakeRange(0, 5)];
label.attributedText = mutaString;
}
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
// Do any additional setup after loading the view.
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
}
11.图片变灰度
-(UIImage *) grayscaleImage: (UIImage *) image
{
CGSize size = image.size;
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width,
image.size.height);
// Create a mono/gray color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, size.width,
size.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
// Draw the image into the grayscale context
CGContextDrawImage(context, rect, [image CGImage]);
CGImageRef grayscale = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Recover the image
UIImage *img = [UIImage imageWithCGImage:grayscale];
CFRelease(grayscale);
return img;
}
13.16进制转rgb
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
# ios获得屏幕图像
# ios
# 压缩图片
# 加边框
# 调整label的size
# 时间戳
# 详解IOS图片压缩处理
# iOS开发之image图片压缩及压缩成指定大小的两种方法
# iOS实现压缩图片上传功能
# iOS 图片压缩方法的示例代码
# iOS实现图片压缩的两种方法及图片压缩上传功能
# 详解IOS开发中图片上传时两种图片压缩方式的比较
# iOS图片压缩、滤镜、剪切及渲染等详解
# 转化为
# 转化成
# 在这个
# 所需
# 创建一个
# integerValue
# NSDate
# stringFromDate
# NSLog
# inColorString
# result
# unsigned
# colorFromHexRGB
# UIColor
# date
# setDateFormat
# YY
# MM
# formatter
# TimeTrasformWithDate
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
怎样使用JSON进行数据交换_它有什么限制
Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法
Win11应用商店下载慢怎么办 Win11更改DNS提速下载【修复】
如何在云主机上快速搭建多站点网站?
Windows10电脑怎么查看硬盘通电时间_Win10使用工具检测磁盘健康
公司门户网站制作流程,华为官网怎么做?
Laravel如何处理和验证JSON类型的数据库字段
Laravel如何实现本地化和多语言支持_Laravel多语言配置与翻译文件管理
实例解析angularjs的filter过滤器
zabbix利用python脚本发送报警邮件的方法
安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出
Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】
Thinkphp 中 distinct 的用法解析
Swift中switch语句区间和元组模式匹配
使用豆包 AI 辅助进行简单网页 HTML 结构设计
Laravel Seeder填充数据教程_Laravel模型工厂Factory使用
百度浏览器网页无法复制文字怎么办 百度浏览器复制修复
Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】
🚀拖拽式CMS建站能否实现高效与个性化并存?
网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?
Laravel如何使用缓存系统提升性能_Laravel缓存驱动和应用优化方案
Linux安全能力提升路径_长期防护思维说明【指导】
JS去除重复并统计数量的实现方法
Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】
php8.4header发送头信息失败怎么办_php8.4header函数问题解决【解答】
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
如何用AWS免费套餐快速搭建高效网站?
详解jQuery中的事件
如何在云服务器上快速搭建个人网站?
网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?
,南京靠谱的征婚网站?
如何在建站之星网店版论坛获取技术支持?
Laravel如何配置Horizon来管理队列?(安装和使用)
如何快速选择适合个人网站的云服务器配置?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】
Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验
Laravel如何实现API资源集合?(Resource Collection教程)
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
高端建站三要素:定制模板、企业官网与响应式设计优化
手机网站制作与建设方案,手机网站如何建设?
如何解决hover在ie6中的兼容性问题
如何在香港免费服务器上快速搭建网站?
Android 常见的图片加载框架详细介绍
如何在万网利用已有域名快速建站?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
Python进程池调度策略_任务分发说明【指导】
网站图片在线制作软件,怎么在图片上做链接?
怎么用AI帮你为初创公司进行市场定位分析?
小米17系列还有一款新机?主打6.9英寸大直屏和旗舰级影像

