方法抽取

  • 移除tabbar的子控件
-(void)layoutSubviews
{
    [super layoutSubviews];
    
   //移除系统自带控件
    for (UIView* child in self.subviews) {
        // 取出tabbar中的所有UITabBarButton 设置其frame
        if([child isKindOfClass:NSClassFromString(@"UITabBarButton")]
           || [child isKindOfClass:NSClassFromString(@"_UITabBarBackgroundView")]
           || [child isKindOfClass:NSClassFromString(@"UIImageView")]
           ){
            [child removeFromSuperview];
        }
    }
}
  • 纯颜色转图片
- (UIImage *)imageFromColor:(UIColor *)color{
    CGRect rect = CGRectMake(0, 0, 100, 100);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

  • 截图并保存
//snapshotImage 这个方法效率比较低,
- (UIImage *)snapshotImage {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snap;
}
//iOS 7上UIView上提供了drawViewHierarchyInRect:afterScreenUpdates:来截图,速度比renderInContext:快15倍。
- (UIImage *)snapshotImageAfterScreenUpdates:(BOOL)afterUpdates {
    if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        return [self snapshotImage];
    }
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0);
    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:afterUpdates];
    UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snap;
}
//保存截图到相册
- (void)saveImageToPhotos:(UIImage *)image{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//状态回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (!error) {
        NSLog(@"\n\n保存成功\n\n");
    } else {
        NSLog(@"\n\n保存失败\n\n");
    }
}
  • 图片字符串互相转换
//图片转字符串
-(NSString *)UIImageToBase64Str:(UIImage *) image
{
    NSData *data = UIImageJPEGRepresentation(image, 1.0f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encodedImageStr;
}
//
//字符串转图片
-(UIImage *)Base64StrToUIImage:(NSString *)_encodedImageStr
{
    NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:_encodedImageStr];
    UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
    return _decodedImage;
}
  • 获得汉语拼音首字母
//第一种方法:先将汉字转换为 拼音 再获取首字母
//获取拼音首字母(传入汉字字符串, 返回大写拼音首字母)
/*
 - (NSString *)firstCharactor:(NSString *)aString
 {
 //转成了可变字符串
 NSMutableString *str = [NSMutableString stringWithString:aString];
 //先转换为带声调的拼音
 CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
 //再转换为不带声调的拼音
 CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
 //转化为大写拼音
 NSString *pinYin = [str capitalizedString];
 //获取并返回首字母
 return [pinYin substringToIndex:1];
 }
 //
 //第二种方法:
 NSString *string = @"" ;
 if ([string length])
 {
 NSMutableString *mutableString = [NSMutableString stringWithString:string] ;
 /**
 *  由于此方法是在coreFoundation框架下,
 *  而我们平时所使用的类型都是Foundation框架下的,所以需要转换类型.
 *
 *  @param string#>    string 所需要转换的原字符#>
 *  @param range#>     range 所需要转换字符的范围.
 *      如果为0或者是NULL意思是所有字符都转换#>
 *  @param transform#> transform 转换方式#>
 *  @param reverse#>   reverse 如果为YES,返回原字符串;
 *      如果为NO,返回转换之后的字符串#>
 *
 *  @return return value description
 */
// 将所有非英文的字符转换为拉丁字母,并且带声调和重音标识
// __bridge :只改变当前对象的类型,但是不改变对象内存的管理权限
CFStringTransform((__bridge CFMutableStringRef)mutableString , 0,kCFStringTransformToLatin , NO) ;
// 去掉声调
CFStringTransform((__bridge CFMutableStringRef)mutableString , 0,kCFStringTransformStripDiacritics , NO) ;
// 每个单词的首字母大写 后再截取字符串
NSString *str = [[mutableString capitalizedString] substringToIndex:1];
}

  • 获得lacunchImage
-(NSString *)getLaunchImageName
{
    CGSize viewSize = self.view.bounds.size;
    NSString *viewOrientation = @"Portrait";    //横屏请设置成 @"Landscape"
    NSString *launchImage = nil;
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary* dict in imagesDict)
    {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
        {
            launchImage = dict[@"UILaunchImageName"];
        }
    }
    
    return launchImage;
}

你可能感兴趣的:(方法抽取)