21、image圆角
- (UIImage *)circleImage
{
// NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 1);
// 获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 添加一个圆
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
// 方形变圆形
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 将图片画上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
22、JSON字符串转字典
+ (NSDictionary *)parseJSONStringToNSDictionary:(NSString *)JSONString {
NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
return responseJSON;
}
23、身份证号码验证
- (BOOL)validateIdentityCard {
BOOL flag;
if (self.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:self];
}
24、获取当前显示的控制器
- (UIViewController *)getVisibleViewControllerFrom:(UIViewController*)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self getVisibleViewControllerFrom:[((UINavigationController*) vc) visibleViewController]];
}else if ([vc isKindOfClass:[UITabBarController class]]){
return [self getVisibleViewControllerFrom:[((UITabBarController*) vc) selectedViewController]];
} else {
if (vc.presentedViewController) {
return [self getVisibleViewControllerFrom:vc.presentedViewController];
} else {
return vc;
}
}
}
25、判断当前图片是否存在透明通道
- (BOOL)hasAlphaChannel{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(self.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}
26、根据bundle中的文件名读取图片
+ (UIImage *)imageWithFileName:(NSString *)name {
NSString *extension = @"png";
NSArray *components = [name componentsSeparatedByString:@"."];
if ([components count] >= 2) {
NSUInteger lastIndex = components.count - 1;
extension = [components objectAtIndex:lastIndex];
name = [name substringToIndex:(name.length-(extension.length+1))];
}
// 如果为Retina屏幕且存在对应图片,则返回Retina图片,否则查找普通图片
if ([UIScreen mainScreen].scale == 2.0) {
name = [name stringByAppendingString:@"@2x"];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path != nil) {
return [UIImage imageWithContentsOfFile:path];
}
}
if ([UIScreen mainScreen].scale == 3.0) {
name = [name stringByAppendingString:@"@3x"];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path != nil) {
return [UIImage imageWithContentsOfFile:path];
}
}
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path) {
return [UIImage imageWithContentsOfFile:path];
}
return nil;
}
27、cell去除选中效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
28、cell点按效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
29、移除字符串中的空格和换行
+ (NSString *)removeSpaceAndNewline:(NSString *)str {
NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
return temp;
}
30、判断字符串中是否有空格
+ (BOOL)isBlank:(NSString *)str {
NSRange _range = [str rangeOfString:@" "];
if (_range.location != NSNotFound) {
//有空格
return YES;
} else {
//没有空格
return NO;
}
}
31、获取一个视频的第一帧图片
+ (UIImage *)getFirstFrameWithVideoFile:(NSString *)filepath{
NSURL *url = [NSURL URLWithString:filepath];
AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *one = [[UIImage alloc] initWithCGImage:oneRef];
return one;
}
32、获取视频的时长
+ (NSInteger)getVideoTimeByUrlString:(NSString *)urlString {
NSURL *videoUrl = [NSURL URLWithString:urlString];
AVURLAsset *avUrl = [AVURLAsset assetWithURL:videoUrl];
CMTime time = [avUrl duration];
int seconds = ceil(time.value/time.timescale);
return seconds;
}
33、isKindOfClass和isMemberOfClass的区别
isKindOfClass可以判断某个对象是否属于某个类,或者这个类的子类。
isMemberOfClass更加精准,它只能判断这个对象类型是否为这个类(不能判断子类)
34、一次删除某个按钮的多个点击事件
[youButton removeTarget:nil action:nil forControlEvents:UIControlEventAllEvents];
35、UITextView中打开或禁用复制,剪切,选择,全选等功能
// 继承UITextView重写这个方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
// 返回NO为禁用,YES为开启
// 粘贴
if (action == @selector(paste:)) return NO;
// 剪切
if (action == @selector(cut:)) return NO;
// 复制
if (action == @selector(copy:)) return NO;
// 选择
if (action == @selector(select:)) return NO;
// 选中全部
if (action == @selector(selectAll:)) return NO;
// 删除
if (action == @selector(delete:)) return NO;
// 分享
if (action == @selector(share)) return NO;
return [super canPerformAction:action withSender:sender];
}
36、为一个view添加虚线边框
CAShapeLayer *border = [CAShapeLayer layer];
border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
border.fillColor = nil;
border.lineDashPattern = @[@4, @2];
border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
border.frame = view.bounds;
[view.layer addSublayer:border];
37、将一个image保存在相册中
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
或者
#import <Photos/Photos.h>
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
changeRequest.creationDate = [NSDate date];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"successfully saved");
}
else {
NSLog(@"error saving to photos: %@", error);
}
}];
38、在指定的宽度下,让UILabel自动设置最佳font
label.adjustsFontSizeToFitWidth = YES;
39、在状态栏增加网络请求的菊花,类似safari加载网页的时候状态栏菊花
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
40、不让控制器的view随着控制器的xib拉伸或压缩
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;