Skip to content

Instantly share code, notes, and snippets.

@xtreme-daniel-crampton
Created February 26, 2015 23:04
Show Gist options
  • Save xtreme-daniel-crampton/f9792d8336297af613a0 to your computer and use it in GitHub Desktop.
Save xtreme-daniel-crampton/f9792d8336297af613a0 to your computer and use it in GitHub Desktop.
+ [UIImage noNilImageNamed:]
//
// UIImage+NoNilImageNamed.m
// UARecord
//
// Created by DX131 on 2/26/15.
// Copyright (c) 2015 Under Armour. All rights reserved.
//
#import "UIImage+NoNilImageNamed.h"
#import <objc/runtime.h>
@implementation UIImage (NoNilImageNamed)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
method_exchangeImplementations(class_getClassMethod(self, @selector(imageNamed:)), class_getClassMethod(self, @selector(noNilImageNamed:)));
});
}
#pragma mark - Method Swizzling
+ (instancetype)noNilImageNamed:(NSString *)imageName {
if (imageName && imageName.length) {
return [UIImage noNilImageNamed:imageName];
}
NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString componentsSeparatedByCharactersInSet:separatorSet]];
[array removeObject:@""];
UALog(@"Called [UIImage imageNamed:] with a nil image name from [%@ %@]", array[3], array[4]);
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment