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


相关推荐: Laravel如何创建自定义中间件?(Middleware代码示例)  HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】  Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】  如何正确下载安装西数主机建站助手?  JS经典正则表达式笔试题汇总  详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点  laravel怎么通过契约(Contracts)编程_laravel契约(Contracts)编程方法  宙斯浏览器文件分类查看教程 快速筛选视频文档与图片方法  ,在苏州找工作,上哪个网站比较好?  Laravel如何与Inertia.js和Vue/React构建现代单页应用  消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工  Laravel如何自定义错误页面(404, 500)?(代码示例)  Laravel的辅助函数有哪些_Laravel常用Helpers函数提高开发效率  移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?  ,怎么在广州志愿者网站注册?  如何快速搭建高效可靠的建站解决方案?  长沙企业网站制作哪家好,长沙水业集团官方网站?  Python3.6正式版新特性预览  PHP怎么接收前端传的文件路径_处理文件路径参数接收方法【汇总】  如何基于云服务器快速搭建个人网站?  如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?  微信小程序 五星评分(包括半颗星评分)实例代码  中山网站制作网页,中山新生登记系统登记流程?  Python文件异常处理策略_健壮性说明【指导】  宙斯浏览器视频悬浮窗怎么开启 边看视频边操作其他应用教程  Laravel如何实现文件上传和存储?(本地与S3配置)  Laravel如何配置Horizon来管理队列?(安装和使用)  如何选择可靠的免备案建站服务器?  Linux系统命令中tree命令详解  重庆市网站制作公司,重庆招聘网站哪个好?  JavaScript中的标签模板是什么_它如何扩展字符串功能  如何确认建站备案号应放置的具体位置?  mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?  Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】  如何构建满足综合性能需求的优质建站方案?  如何用AWS免费套餐快速搭建高效网站?  百度浏览器网页无法复制文字怎么办 百度浏览器复制修复  Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道  通义万相免费版怎么用_通义万相免费版使用方法详细指南【教程】  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?  微信小程序 配置文件详细介绍  Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)  EditPlus中的正则表达式 实战(1)  Laravel如何实现用户密码重置功能?(完整流程代码)  如何基于PHP生成高效IDC网络公司建站源码?  Android自定义listview布局实现上拉加载下拉刷新功能  Laravel如何监控和管理失败的队列任务_Laravel失败任务处理与监控  如何快速搭建二级域名独立网站?  Laravel怎么进行数据库事务处理_Laravel DB Facade事务操作确保数据一致性