iOS代码片段

获取UIView的父ViewController

@implementation UIView (GetVCAdditions)
- (UIViewController *)GetiewController {
    Class vcc = [UIViewController class];
    UIResponder *responder = self;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: vcc])
            return (UIViewController *)responder;
    return nil;
}

描述Class

- (NSString *)description
{
  NSString *className = NSStringFromClass([self class]);
  return [NSString stringWithFormat:@"<%@: %p %@ - %d>", className, self, self.playerID, self.score];
}


更有用的LOG

#define LOG(fmt, ...) NSLog(@"%s:%d (%s): " fmt, __FILE__, __LINE__, __func__, ## __VA_ARGS__)

UIColor 转 UIImage

- (UIImage *) createImageWithColor: (UIColor *) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}


你可能感兴趣的:(iOS代码片段)