Skip to content

Instantly share code, notes, and snippets.

View axldyb's full-sized avatar
💭
Opening doors with digital keys @Unloc

Aksel Dybdal axldyb

💭
Opening doors with digital keys @Unloc
View GitHub Profile
@axldyb
axldyb / UIImage crop to square
Created April 29, 2015 09:42
Extension for cropping an UIImage to a square.
extension UIImage {
func cropsToSquare() -> UIImage {
let refWidth = CGFloat(CGImageGetWidth(self.CGImage))
let refHeight = CGFloat(CGImageGetHeight(self.CGImage))
let cropSize = refWidth > refHeight ? refHeight : refWidth
let x = (refWidth - cropSize) / 2.0
let y = (refHeight - cropSize) / 2.0
let cropRect = CGRectMake(x, y, cropSize, cropSize)
@axldyb
axldyb / Darwin
Last active April 29, 2020 17:46
Darwin notifications listen and send for iOS
#import <Foundation/Foundation.h>
typedef void (^DarwinNotificationBlock)(NSString *identifier);
@interface Darwin : NSObject
+ (Darwin *)sharedInstance;
- (void)startListening:(DarwinNotificationBlock)block;
- (void)test;
@axldyb
axldyb / Listen for all notifications iOS-Swift
Created April 27, 2015 09:02
Used to listen for all notifications on iOS.
NSNotificationCenter.defaultCenter().addObserverForName(nil, object: nil, queue: nil) { (notification) in
NSLog("Notification found with:\r\n name: \(notification.name)\r\n object: \(notification.object)\r\n userInfo: \(notification.userInfo)")
}
let className = toString(object.dynamicType).componentsSeparatedByString(".").last!
// Overriding NSLocalizedString to use NSLocalizedStringFromTableInBundle instead
#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]
#ifdef NSLocalizedString
#undef NSLocalizedString
#endif
#define NSLocalizedString(key, comment) NSLocalizedStringFromTableInBundle(key, nil, currentLanguageBundle, comment)
@axldyb
axldyb / gist:7765269
Created December 3, 2013 07:24
Method retrying AFNetworking request n number of times. Not a perfect implementation, but this is just for saving the idea.
- (void)downloadFileRetryingNumberOfTimes:(NSUInteger)numberOfTimes
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
if (numberOfTimes <= 0) {
if (failure) {
NSError *error = ...;
failure(error);
}
} else {
@axldyb
axldyb / gist:3979272
Created October 30, 2012 09:37
Background queue block using GCD to create a thread for us
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue,^{
// Do magical stuff in the background
dispatch_async(mainQueue,^{
// Update UI in the main thread