Skip to content

Instantly share code, notes, and snippets.

View luca-bernardi's full-sized avatar

Luca Bernardi luca-bernardi

View GitHub Profile
@luca-bernardi
luca-bernardi / gist:968154
Created May 12, 2011 08:17
UIImage scale and rotate based on exif flag
UIImage *scaleAndRotateImage(UIImage *image)
{
int kMaxResolution = 1920;
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
@luca-bernardi
luca-bernardi / NSDateFormatterInstanceCache.mm
Last active December 10, 2015 13:04
Threadsafe NSDateFormatter's instance cache
/**
As Apple said [NSDateFormatter init] is very expensive (https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW10)
In Apple's code is used a static const, but since NSDateFormatter
isn't thread safe a better approach is to use Thread local store (http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html#//apple_ref/doc/uid/10000057i-CH15-SW4)
to cache the NSDateFormatter instance
*/
NSString * const kCachedDateFormatterKey = @"CachedDateFormatterKey";
+ (NSDateFormatter *)dateFormatter
@luca-bernardi
luca-bernardi / DownloadImageTask.java
Created January 28, 2013 13:38
Android's AsyncTask for download asynchronously an image from an URL and assign it to an ImageView
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
@luca-bernardi
luca-bernardi / TheEleganceOfBlock.mm
Created February 18, 2013 10:42
How to use inline block to better organize a block of code
__weak typeof(self) weakSelf = self;
self.collectionView = (UICollectionView *)^{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:weakSelf.view.bounds
collectionViewLayout:flowLayout];
collectionView.delegate = weakSelf;
collectionView.dataSource = weakSelf;
return collectionView;
@luca-bernardi
luca-bernardi / AVAsset+VideoOrientation.h
Created February 23, 2013 18:12
Find the video orientation of an AVAsset. (Useful if you need to send the video to a remote server)
//
// AVAsset+VideoOrientation.h
//
// Created by Luca Bernardi on 19/09/12.
// Copyright (c) 2012 Luca Bernardi. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
typedef enum {
LBVideoOrientationUp, //Device starts recording in Portrait
import lldb
import commands
import optparse
import shlex
def __lldb_init_module (debugger, dict):
debugger.HandleCommand('command script add -f windowDescription.window_description_command window_description')
print 'The "window_description" command has been installed'
#import <Foundation/Foundation.h>
@interface LBRObject : NSObject
@end
@implementation LBRObject
- (void)execute
{
@synchronized(self) {
-- CIS 194: Homework 1 (http://www.seas.upenn.edu/~cis194/hw/01-intro.pdf)
-- toDigits 1234 == [1,2,3,4]
-- toDigitsRev 1234 == [4,3,2,1]
-- toDigits 0 == []
-- toDigits (-17) == []
toDigitsRev :: Integer -> [Integer]
toDigitsRev x = if x <= 0
import lldb
import commands
def __lldb_init_module (debugger, dict):
debugger.HandleCommand('command script add -f LBRShortcut.window_description_command window_description')
debugger.HandleCommand('command script add -f LBRShortcut.json_data_command json_data')
debugger.HandleCommand('command script add -f LBRShortcut.fire_fault_command fire_fault')
def window_description_command(debbuger, command, result, dict):
@luca-bernardi
luca-bernardi / objc_setProperty_nonatomic_copy
Created August 13, 2014 10:44
objc_setProperty_nonatomic_copy disassembled
void objc_setProperty_nonatomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
{
temp = [newValue copyWithZone:NULL];
oldValue = *(self + offset);
*(sef + offset) = temp;
_objc_release(oldValue);
}