1. 獲取磁盤總空間大小api
//磁盤總空間
+ (CGFloat)diskOfAllSizeMBytes{
CGFloat size = 0.0;
NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"error: %@", error.localizedDescription);
#endif
}else{
NSNumber *number = [dic objectForKey:NSFileSystemSize];
size = [number floatValue]/1024/1024;
}
return size;
}數組
2. 獲取磁盤可用空間大小spa
//磁盤可用空間
+ (CGFloat)diskOfFreeSizeMBytes{
CGFloat size = 0.0;
NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"error: %@", error.localizedDescription);
#endif
}else{
NSNumber *number = [dic objectForKey:NSFileSystemFreeSize];
size = [number floatValue]/1024/1024;
}
return size;
}orm
5. 獲取字符串(或漢字)首字母排序
//獲取字符串(或漢字)首字母
+ (NSString *)firstCharacterWithString:(NSString *)string{
NSMutableString *str = [NSMutableString stringWithString:string];
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
NSString *pingyin = [str capitalizedString];
return [pingyin substringToIndex:1];
}索引
6. 將字符串數組按照元素首字母順序進行排序分組圖片
//將字符串數組按照元素首字母順序進行排序分組
+ (NSDictionary *)dictionaryOrderByCharacterWithOriginalArray:(NSArray *)array{
if (array.count == 0) {
return nil;
}
for (id obj in array) {
if (![obj isKindOfClass:[NSString class]]) {
return nil;
}
}
UILocalizedIndexedCollation *indexedCollation = [UILocalizedIndexedCollation currentCollation];
NSMutableArray *objects = [NSMutableArray arrayWithCapacity:indexedCollation.sectionTitles.count];
//建立27個分組數組
for (int i = 0; i < indexedCollation.sectionTitles.count; i++) {
NSMutableArray *obj = [NSMutableArray array];
[objects addObject:obj];
}
NSMutableArray *keys = [NSMutableArray arrayWithCapacity:objects.count];
//按字母順序進行分組
NSInteger lastIndex = -1;
for (int i = 0; i < array.count; i++) {
NSInteger index = [indexedCollation sectionForObject:array[i] collationStringSelector:@selector(uppercaseString)];
[[objects objectAtIndex:index] addObject:array[i]];
lastIndex = index;
}
//去掉空數組
for (int i = 0; i < objects.count; i++) {
NSMutableArray *obj = objects[i];
if (obj.count == 0) {
[objects removeObject:obj];
}
}
//獲取索引字母
for (NSMutableArray *obj in objects) {
NSString *str = obj[0];
NSString *key = [self firstCharacterWithString:str];
[keys addObject:key];
}
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:objects forKey:keys];
return dic;
}
//獲取字符串(或漢字)首字母
+ (NSString *)firstCharacterWithString:(NSString *)string{
NSMutableString *str = [NSMutableString stringWithString:string];
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
NSString *pingyin = [str capitalizedString];
return [pingyin substringToIndex:1];
}ip
7. 獲取當前時間ci
//獲取當前時間
//format: @"yyyy-MM-dd HH:mm:ss"、@"yyyy年MM月dd日 HH時mm分ss秒"
+ (NSString *)currentDateWithFormat:(NSString *)format{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
return [dateFormatter stringFromDate:[NSDate date]];
}rem
8. 計算上第二天期距離如今多久, 如 xx 小時前、xx 分鐘前等
/**
* 計算上第二天期距離如今多久
*
* @param lastTime 上第二天期(須要和格式對應)
* @param format1 上第二天期格式
* @param currentTime 最近日期(須要和格式對應)
* @param format2 最近日期格式
*
* @return xx分鐘前、xx小時前、xx天前
*/
+ (NSString *)timeIntervalFromLastTime:(NSString *)lastTime
lastTimeFormat:(NSString *)format1
ToCurrentTime:(NSString *)currentTime
currentTimeFormat:(NSString *)format2{
//上次時間
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
dateFormatter1.dateFormat = format1;
NSDate *lastDate = [dateFormatter1 dateFromString:lastTime];
//當前時間
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.dateFormat = format2;
NSDate *currentDate = [dateFormatter2 dateFromString:currentTime];
return [Utilities timeIntervalFromLastTime:lastDate ToCurrentTime:currentDate];
}
+ (NSString *)timeIntervalFromLastTime:(NSDate *)lastTime ToCurrentTime:(NSDate *)currentTime{
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
//上次時間
NSDate *lastDate = [lastTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:lastTime]];
//當前時間
NSDate *currentDate = [currentTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:currentTime]];
//時間間隔
NSInteger intevalTime = [currentDate timeIntervalSinceReferenceDate] - [lastDate timeIntervalSinceReferenceDate];
//秒、分、小時、天、月、年
NSInteger minutes = intevalTime / 60;
NSInteger hours = intevalTime / 60 / 60;
NSInteger day = intevalTime / 60 / 60 / 24;
NSInteger month = intevalTime / 60 / 60 / 24 / 30;
NSInteger yers = intevalTime / 60 / 60 / 24 / 365;
if (minutes <= 10) {
return @"剛剛";
}else if (minutes < 60){
return [NSString stringWithFormat: @"%ld分鐘前",(long)minutes];
}else if (hours < 24){
return [NSString stringWithFormat: @"%ld小時前",(long)hours];
}else if (day < 30){
return [NSString stringWithFormat: @"%ld天前",(long)day];
}else if (month < 12){
NSDateFormatter * df =[[NSDateFormatter alloc]init];
df.dateFormat = @"M月d日";
NSString * time = [df stringFromDate:lastDate];
return time;
}else if (yers >= 1){
NSDateFormatter * df =[[NSDateFormatter alloc]init];
df.dateFormat = @"yyyy年M月d日";
NSString * time = [df stringFromDate:lastDate];
return time;
}
return @"";
}
12. 對圖片進行濾鏡處理
#pragma mark - 對圖片進行濾鏡處理
// 懷舊 --> CIPhotoEffectInstant 單色 --> CIPhotoEffectMono
// 黑白 --> CIPhotoEffectNoir 褪色 --> CIPhotoEffectFade
// 色調 --> CIPhotoEffectTonal 沖印 --> CIPhotoEffectProcess
// 歲月 --> CIPhotoEffectTransfer 鉻黃 --> CIPhotoEffectChrome
// CILinearToSRGBToneCurve, CISRGBToneCurveToLinear, CIGaussianBlur, CIBoxBlur, CIDiscBlur, CISepiaTone, CIDepthOfField
+ (UIImage *)filterWithOriginalImage:(UIImage *)image filterName:(NSString *)name{
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithImage:image];
CIFilter *filter = [CIFilter filterWithName:name];
[filter setValue:inputImage forKey:kCIInputImageKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
UIImage *resultImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return resultImage;
}
13. 對圖片進行模糊處理
#pragma mark - 對圖片進行模糊處理
// CIGaussianBlur ---> 高斯模糊
// CIBoxBlur ---> 均值模糊(Available in iOS 9.0 and later)
// CIDiscBlur ---> 環形卷積模糊(Available in iOS 9.0 and later)
// CIMedianFilter ---> 中值模糊, 用於消除圖像噪點, 無需設置radius(Available in iOS 9.0 and later)
// CIMotionBlur ---> 運動模糊, 用於模擬相機移動拍攝時的掃尾效果(Available in iOS 9.0 and later)
+ (UIImage *)blurWithOriginalImage:(UIImage *)image blurName:(NSString *)name radius:(NSInteger)radius{
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithImage:image];
CIFilter *filter;
if (name.length != 0) {
filter = [CIFilter filterWithName:name];
[filter setValue:inputImage forKey:kCIInputImageKey];
if (![name isEqualToString:@"CIMedianFilter"]) {
[filter setValue:@(radius) forKey:@"inputRadius"];
}
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
UIImage *resultImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return resultImage;
}else{
return nil;
}
}
14. 調整圖片飽和度、亮度、對比度
/**
* 調整圖片飽和度, 亮度, 對比度
*
* @param image 目標圖片
* @param saturation 飽和度
* @param brightness 亮度: -1.0 ~ 1.0
* @param contrast 對比度
*
*/
+ (UIImage *)colorControlsWithOriginalImage:(UIImage *)image
saturation:(CGFloat)saturation
brightness:(CGFloat)brightness
contrast:(CGFloat)contrast{
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithImage:image];
CIFilter *filter = [CIFilter filterWithName:@"CIColorControls"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:@(saturation) forKey:@"inputSaturation"];
[filter setValue:@(brightness) forKey:@"inputBrightness"];// 0.0 ~ 1.0
[filter setValue:@(contrast) forKey:@"inputContrast"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
UIImage *resultImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return resultImage;
}
15. 建立一張實時模糊效果 View (毛玻璃效果)
//Avilable in iOS 8.0 and later
+ (UIVisualEffectView *)effectViewWithFrame:(CGRect)frame{
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = frame;
return effectView;
}
16. 全屏截圖
//全屏截圖
+ (UIImage *)shotScreen{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIGraphicsBeginImageContext(window.bounds.size);
[window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
17. 截取一張 view 生成圖片
//截取view生成一張圖片
+ (UIImage *)shotWithView:(UIView *)view{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
18. 截取view中某個區域生成一張圖片
//截取view中某個區域生成一張圖片
+ (UIImage *)shotWithView:(UIView *)view scope:(CGRect)scope{
CGImageRef imageRef = CGImageCreateWithImageInRect([self shotWithView:view].CGImage, scope);
UIGraphicsBeginImageContext(scope.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, scope.size.width, scope.size.height);
CGContextTranslateCTM(context, 0, rect.size.height);//下移
CGContextScaleCTM(context, 1.0f, -1.0f);//上翻
CGContextDrawImage(context, rect, imageRef);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
CGContextRelease(context);
return image;
}
19. 壓縮圖片到指定尺寸大小
//壓縮圖片到指定尺寸大小
+ (UIImage *)compressOriginalImage:(UIImage *)image toSize:(CGSize)size{
UIImage *resultImage = image;
UIGraphicsBeginImageContext(size);
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIGraphicsEndImageContext();
return resultImage;
}
26. 繪製虛線
/* ** lineFrame: 虛線的 frame ** length: 虛線中短線的寬度 ** spacing: 虛線中短線之間的間距 ** color: 虛線中短線的顏色 */ + (UIView *)createDashedLineWithFrame:(CGRect)lineFrame lineLength:(int)length lineSpacing:(int)spacing lineColor:(UIColor *)color{ UIView *dashedLine = [[UIView alloc] initWithFrame:lineFrame]; dashedLine.backgroundColor = [UIColor clearColor]; CAShapeLayer *shapeLayer = [CAShapeLayer layer]; [shapeLayer setBounds:dashedLine.bounds]; [shapeLayer setPosition:CGPointMake(CGRectGetWidth(dashedLine.frame) / 2, CGRectGetHeight(dashedLine.frame))]; [shapeLayer setFillColor:[UIColor clearColor].CGColor]; [shapeLayer setStrokeColor:color.CGColor]; [shapeLayer setLineWidth:CGRectGetHeight(dashedLine.frame)]; [shapeLayer setLineJoin:kCALineJoinRound]; [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:length], [NSNumber numberWithInt:spacing], nil]]; CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 0, 0); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(dashedLine.frame), 0); [shapeLayer setPath:path]; CGPathRelease(path); [dashedLine.layer addSublayer:shapeLayer]; return dashedLine; }