天天看點

iOS開發中的一些常用方法

1.磁盤總空間大小

+ (CGFloat)diskOfAllSizeMBytes
{
    CGFloat size = ;
    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]//;
    }
    return size;
}


           

2.磁盤可用空間大小

+ (CGFloat)diskOfFreeSizeMBytes
{
    CGFloat size = ;
    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]//;
    }
    return size;
}

           

3.将字元串數組按照元素首字母順序進行排序分組

+ (NSDictionary *)dictionaryOrderByCharacterWithOriginalArray:(NSArray *)array
{
    if (array.count == ) {
        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 = ; i < indexedCollation.sectionTitles.count; i++) {
        NSMutableArray *obj = [NSMutableArray array];
        [objects addObject:obj];
    }
    NSMutableArray *keys = [NSMutableArray arrayWithCapacity:objects.count];
    //按字母順序進行分組
    NSInteger lastIndex = -;
    for (int i = ; i < array.count; i++) {
        NSInteger index = [indexedCollation sectionForObject:array[i] collationStringSelector:@selector(uppercaseString)];
        [[objects objectAtIndex:index] addObject:array[i]];
        lastIndex = index;
    }
    //去掉空數組
    for (int i = ; i < objects.count; i++) {
        NSMutableArray *obj = objects[i];
        if (obj.count == ) {
            [objects removeObject:obj];
        }
    }
    //擷取索引字母
    for (NSMutableArray *obj in objects) {
        NSString *str = obj[];
        NSString *key = [self firstCharacterWithString:str];
        [keys addObject:key];
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:objects forKey:keys];
    return dic;
}

           

4.将字元串數組按照元素首字母順序進行排序分組

+ (NSDictionary *)dictionaryOrderByCharacterWithOriginalArray:(NSArray *)array
{
    if (array.count == ) {
        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 = ; i < indexedCollation.sectionTitles.count; i++) {
        NSMutableArray *obj = [NSMutableArray array];
        [objects addObject:obj];
    }
    NSMutableArray *keys = [NSMutableArray arrayWithCapacity:objects.count];
    //按字母順序進行分組
    NSInteger lastIndex = -;
    for (int i = ; i < array.count; i++) {
        NSInteger index = [indexedCollation sectionForObject:array[i] collationStringSelector:@selector(uppercaseString)];
        [[objects objectAtIndex:index] addObject:array[i]];
        lastIndex = index;
    }
    //去掉空數組
    for (int i = ; i < objects.count; i++) {
        NSMutableArray *obj = objects[i];
        if (obj.count == ) {
            [objects removeObject:obj];
        }
    }
    //擷取索引字母
    for (NSMutableArray *obj in objects) {
        NSString *str = obj[];
        NSString *key = [self firstCharacterWithString:str];
        [keys addObject:key];
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:objects forKey:keys];
    return dic;
}

           

5.對圖檔進行濾鏡處理

// 懷舊 --> 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;
}

           

6.對圖檔進行模糊處理

// 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;
    }
}

           

7.跳轉到系統的相關界面:

/*
 *  需要添加一個字段
 *  藍色的項目工程檔案 -> Info -> URL Types -> 添加一個 -> 設定URL  Sch**** 為 prefs的url

 NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"];
 [[UIApplication sharedApplication] openURL:url];

 跳轉到其他的界面的字段(不全,詳細看連結)
 About — prefs:root=General&path=About

 Accessibility — prefs:root=General&path=ACCESSIBILITY

 AirplaneModeOn— prefs:root=AIRPLANE_MODE

 Auto-Lock — prefs:root=General&path=AUTOLOCK

 Brightness — prefs:root=Brightness

 Bluetooth — prefs:root=General&path=Bluetooth

 Date& Time — prefs:root=General&path=DATE_AND_TIME

 FaceTime — prefs:root=FACETIME

 General— prefs:root=General

 原文連結:http://www.jianshu.com/p/f48309b

 */

           

8.建立一張實時模糊效果 View (毛玻璃效果)

//Avilable in iOS  and later
+ (UIVisualEffectView *)effectViewWithFrame:(CGRect)frame
{
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = frame;
    return effectView;
}


           

9.設定Label的行間距

+ (void)setLineSpaceWithString:(UILabel *)label
{

    NSMutableAttributedString *attributedString =
    [[NSMutableAttributedString alloc] initWithString:label.text];
    NSMutableParagraphStyle *paragraphStyle =  [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:];

    //調整行間距
    [attributedString addAttribute:NSParagraphStyleAttributeName
                             value:paragraphStyle
                             range:NSMakeRange(, [label.text length])];
    label.attributedText = attributedString;
}


           

10.讓Plain風格的TableView的區頭可以”不懸停”(可以直接百度搜到):

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
     if(scrollView == self.myTab) {
         CGFloat sectionHeaderHeight = ;
         if (scrollView.contentOffset.y=) {

             scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, , , );
         } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {

             scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, , , );
         }
     }
}
           

http://www.cocoachina.com/ios/20160630/16877.html