Skip to content

Instantly share code, notes, and snippets.

@mamaz
mamaz / ScaledToFillSize.m
Created June 24, 2014 07:12
ScaledToFillSize
// Return image Image scaledToFillSize, perfect for thumbnailing
// taken from http://stackoverflow.com/a/17884863/1328982
+ (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size
{
CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
CGFloat width = image.size.width * scale;
CGFloat height = image.size.height * scale;
CGRect imageRect = CGRectMake((size.width - width)/2.0f,
(size.height - height)/2.0f,
width,
// Get string time duration from 2 different NSDate object
- (NSString *)getNaturalLanguageForDate:(NSDate *)date1 andDate:(NSDate *)date2 withCalendar:(NSCalendar *)sysCalendar
{
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
// Create a generated tiny UIImage with color
// Created not by me.
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
// Create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect); // Fill it with your color
+(CGSize)getLabelSizeWordWrapWithString:(NSString*)string font:(UIFont*)font
constrainedToSize:(CGSize)constrainedSize
{
if (IOS_VERSION_LESS_THAN_7) {
CGSize size = [string sizeWithFont:font
constrainedToSize:constrainedSize
lineBreakMode:NSLineBreakByWordWrapping];
return size;
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
FBRequest *met = [FBRequest requestWithGraphPath:@"/me/feed" parameters:nil HTTPMethod:@"GET"];
[met startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// dapetin result
}];
}];
@mamaz
mamaz / gist:6099633
Created July 28, 2013 18:48
Penjelasan Simple Merge Sort
Merge Sort
Merge sort sebenarnya ya sorting mengurutkan dari kecil ke besar dari besar ke kecil, implementasinya yg disorting itu bisa angka (int, float atau double), bisa juga karakter ASCII atau Unicode.
Bedanya sorting di sini menggunakan prinsip divide and conquer atau devide et impera :D
Untuk memahami merge sort, perlu pemahaman tentang fungsi rekursif, kalo belom tahu ya belajar dulu tentang rekursif.
Ada tiga step penting di merge sort, yaitu:
1. Sort setengah bagian kiri dari array. (rekursif)