iOS开发小知识点整理第二期(持续更新)

1.根据时间戳显示不同的时间,如果是今年的时间,则显示月日时分秒,如果是今天的时间,显示时分秒,如果不是今年的时间,则显示年月日时分秒

NSTimeIntervaltime = [timerInterval doubleValue]/1000; NSDate *currentDate=[NSDate dateWithTimeIntervalSince1970:time]; //判断是否是今年 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy"; NSString *nowYear = [fmt stringFromDate:[NSDate date]]; NSString *selfYear = [fmt stringFromDate:currentDate]; if ([nowYear isEqualToString:selfYear]) {//是今年 //判断是否是今天 NSDateFormatter *fmt1 = [[NSDateFormatter alloc] init]; fmt1.dateFormat = @"yyyyMMdd"; NSString *nowString = [fmt1 stringFromDate:[NSDate date]]; NSString *selfString = [fmt1 stringFromDate:currentDate]; if ([nowString isEqualToString:selfString]) {//是今天 显示时分秒 NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"HH:mm:ss"]; return [dateformatter stringFromDate:currentDate]; }else{//不是今天 显示月日 时分秒 NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"MM-dd HH:mm:ss"]; return [dateformatter stringFromDate:currentDate]; }}else{//不是今年,返回时间的格式为年月日形式 NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; return [dateformatter stringFromDate:currentDate]; }

2.将16进制的字符串转换为UIColor的方法
+ (UIColor *)colorWithHexString:(NSString *)hexColorString { if ([hexColorString length] < 6) { //长度不合法 return [UIColor blackColor]; } NSString *tempString = [hexColorString lowercaseString]; if ([tempString hasPrefix:@"0x"]) { //检查开头是0x tempString = [tempString substringFromIndex:2]; } else if ([tempString hasPrefix:@"#"]) { //检查开头是# tempString = [tempString substringFromIndex:1]; } if ([tempString length] != 6) { return [UIColor blackColor]; } //分解三种颜色的值 NSRange range = NSMakeRange(0, 2); NSString *rString = [tempString substringWithRange:range]; range.location = 2; NSString *gString = [tempString substringWithRange:range]; range.location = 4; NSString *bString = [tempString substringWithRange:range]; //取三种颜色值 unsigned int r, g, b; [[NSScanner scannerWithString:rString] scanHexInt:&r]; [[NSScanner scannerWithString:gString] scanHexInt:&g]; [[NSScanner scannerWithString:bString] scanHexInt:&b]; return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:1.0f]; }

3.获取网络视频的时间,将其转换为时分格式
- (NSString *)getVideoInfoWithSourcePath:(NSString *)path{ AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL URLWithString:path]]; CMTimetime = [asset duration]; int seconds = ceil(time.value/time.timescale); int second = seconds % 60; int minutes = (seconds / 60) % 60; return [NSString stringWithFormat:@"%02d:%02d", minutes, second]; }

4.裁剪网络图片,使其显示在控件上不变形
+(UIImage *)getImageFromUrl:(NSURL *)imgUrl imgViewWidth:(CGFloat)width imgViewHeight:(CGFloat)height{UIImage * newImage = [self getImageFromUrl:imgUrl imgViewWidth:width imgViewHeight:height]; return newImage; } //裁剪图片 - (UIImage *)cutImage:(UIImage*)image imgViewWidth:(CGFloat)width imgViewHeight:(CGFloat)height { //压缩图片CGSize newSize; CGImageRef imageRef = nil; if ((image.size.width / image.size.height) < (width / height)) {newSize.width = image.size.width; newSize.height = image.size.width * height /width; imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height)); } else {newSize.height = image.size.height; newSize.width = image.size.height * width / height; imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height)); } return [UIImage imageWithCGImage:imageRef]; }

5.图片裁剪
-(UIImage *) scaleImage: (UIImage *)image scaleFactor:(float)scaleBy { if (image.size.width>1000) { scaleBy = 1000/image.size.width; }else{ scaleBy= 1.0; } CGSize size = CGSizeMake(image.size.width * scaleBy, image.size.height * scaleBy); UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); CGAffineTransform transform = CGAffineTransformIdentity; transform = CGAffineTransformScale(transform, scaleBy, scaleBy); CGContextConcatCTM(context, transform); [image drawAtPoint:CGPointMake(0.0f, 0.0f)]; UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newimg; }

6.将时间字符串转换为时间戳(format必需和时间字符串时间的格式保持一致)
-(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:format]; NSDate *date = [dateFormatter dateFromString:formatTime]; //时间转时间戳的方法:NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue]; NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值 return timeSp; }

7.判断手机号的合法性
+ (BOOL)isMobileNumber:(NSString *)mobileNum { if (mobileNum.length != 11) { return NO; } /** * 手机号码: * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9] * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188 * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186 * 电信号段: 133,149,153,170,173,177,180,181,189 */ NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$"; /** * 中国移动:China Mobile * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188 */ NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$"; /** * 中国联通:China Unicom * 130,131,132,145,155,156,170,171,175,176,185,186 */ NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$"; /** * 中国电信:China Telecom * 133,149,153,170,173,177,180,181,189 */ NSString *CT = @"^1(3[3]|4[9]|53|7[037]|8[019])\\d{8}$"; NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE]; NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM]; NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU]; NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT]; if (([regextestmobile evaluateWithObject:mobileNum] == YES) || ([regextestcm evaluateWithObject:mobileNum] == YES) || ([regextestct evaluateWithObject:mobileNum] == YES) || ([regextestcu evaluateWithObject:mobileNum] == YES)) { return YES; } else { return NO; } }

8.将字符串进行MD5加密
+ (NSString *)md5EncryptWithString:(NSString *)string {return [self md5:[NSString stringWithFormat:@"%@", string]]; }+ (NSString *)md5:(NSString *)string{ const char *cStr = [string UTF8String]; unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5(cStr, (CC_LONG)strlen(cStr), digest); NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [result appendFormat:@"%02X", digest[i]]; }NSLog(@"----%@",result); return result; }

9.手机类型的判断
+ (NSString *)iphoneType {struct utsname systemInfo; uname(&systemInfo); NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding]; if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s"; if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE"; if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7"; if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus"; if ([platform isEqualToString:@"iPod1,1"])return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"])return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"])return @"iPod Touch 3G"; if ([platform isEqualToString:@"iPod4,1"])return @"iPod Touch 4G"; if ([platform isEqualToString:@"iPod5,1"])return @"iPod Touch 5G"; if ([platform isEqualToString:@"iPad1,1"])return @"iPad 1G"; if ([platform isEqualToString:@"iPad2,1"])return @"iPad 2"; if ([platform isEqualToString:@"iPad2,2"])return @"iPad 2"; if ([platform isEqualToString:@"iPad2,3"])return @"iPad 2"; if ([platform isEqualToString:@"iPad2,4"])return @"iPad 2"; if ([platform isEqualToString:@"iPad2,5"])return @"iPad Mini 1G"; if ([platform isEqualToString:@"iPad2,6"])return @"iPad Mini 1G"; if ([platform isEqualToString:@"iPad2,7"])return @"iPad Mini 1G"; if ([platform isEqualToString:@"iPad3,1"])return @"iPad 3"; if ([platform isEqualToString:@"iPad3,2"])return @"iPad 3"; if ([platform isEqualToString:@"iPad3,3"])return @"iPad 3"; if ([platform isEqualToString:@"iPad3,4"])return @"iPad 4"; if ([platform isEqualToString:@"iPad3,5"])return @"iPad 4"; if ([platform isEqualToString:@"iPad3,6"])return @"iPad 4"; if ([platform isEqualToString:@"iPad4,1"])return @"iPad Air"; if ([platform isEqualToString:@"iPad4,2"])return @"iPad Air"; if ([platform isEqualToString:@"iPad4,3"])return @"iPad Air"; if ([platform isEqualToString:@"iPad4,4"])return @"iPad Mini 2G"; if ([platform isEqualToString:@"iPad4,5"])return @"iPad Mini 2G"; if ([platform isEqualToString:@"iPad4,6"])return @"iPad Mini 2G"; if ([platform isEqualToString:@"i386"])return @"iPhone Simulator"; if ([platform isEqualToString:@"x86_64"])return @"iPhone Simulator"; return platform; }

10.UITableView分类添加方法,实现没有数据界面展示
-(NSInteger)showImageWithName:(NSString *)imageName showMessageWithTitle:(NSString *)title byDataArrayCount:(NSInteger)count withTop:(CGFloat)top withImageWidth:(CGFloat)imageWidth withImageHeight:(CGFloat)imageHeigt {if (count == 0) {self.backgroundView = ({UIView *backView = [[UIView alloc] init]; backView.frame = self.backgroundView.frame; UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:imageName]; [backView addSubview:imageView]; UILabel *label = [[UILabel alloc] init]; label.text = title; [UILabel setLabelTextColor:UIColorFromRGBValue(0x959595) AndFontSize:[self autoScaleW:14] WithLabel:label]; label.textAlignment = NSTextAlignmentCenter; [backView addSubview:label]; [imageView mas_makeConstraints:^(MASConstraintMaker *make) {make.leading.equalTo(backView).offset((screenW - [self autoScaleW:imageWidth])/2); make.top.equalTo([self autoScaleH:top]); make.width.equalTo([self autoScaleW:imageWidth]); make.height.equalTo([self autoScaleH:imageHeigt]); }]; [label mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(imageView.bottom).equalTo(10); make.leading.equalTo(0); make.width.equalTo(screenW); make.height.equalTo(21); }]; backView; }); }else{self.backgroundView = nil; }return count; }

    推荐阅读