iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
来源:互联网 发布:战斗数据预测数据库 编辑:程序博客网 时间:2024/05/16 12:36
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;
unsignedint colorCode = 0;
unsignedchar redByte, greenByte, blueByte;
if (nil != inColorString) {
NSScanner *scanner = [NSScanner scannerWithString:inColorString];
(void) [scanner scanHexInt:&colorCode]; // ignore error
}
redByte = (unsignedchar) (colorCode >> 16);
greenByte = (unsignedchar) (colorCode >> 8);
blueByte = (unsignedchar) (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的placeho
[textFsetValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[textFsetValue:[UIFont boldSystemFontOfSize:15]forKeyPath:@"_placeholderLabel.font"];
8、调用此方法改变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、图片灰度
-(UIImage*) grayscaleImage: (UIImage *)image{
CGSizesize =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;
}
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- iOS - 常用到的图片功能:截取当前屏幕/选择本地图片/图片压缩
- iOS 获得指定范围内的屏幕图像(截屏)
- iOS动态计算Label的size
- (图片显示加边框)
- iOS 常用的图片压缩函数
- ios 调整Label上文字的行距
- iOS根据屏幕的宽高等比压缩图片
- iOS 常用小功能
- iOS常用小功能
- iOS开发常用的小功能(持续更新)
- iOS常用小功能的实现
- 一个图片压缩并导入服务器的小功能
- ios开发小知识点--显示label上加删除线
- iOS UI_APPEARANCE_SELECTOR、appearence 和UIButton加图片并调整图片和字体的位置
- IOS 获取图片的size
- 图片加边框 (css)
- JNI支持C++与C的区别
- view类某些控件创建时候系统自动帮助我们调用的某些方法
- 重新计算ListView的高度
- 线程挂起,阻塞,休眠理解
- 深入java垃圾回收的详解
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- C++字符换行
- mysql 查询多条结果,合并成一个结果,使用 group_concat()函数
- ToolBar与AppcompatAcitivity实现浸入式Statusbar效果
- JNI相关知识
- Chrome浏览器添加插件Modify Headers for Google Chrome
- 女友闺蜜
- oracle创建表相关语句
- jquery使用ajax加载并解析xml