Skip to content

Instantly share code, notes, and snippets.

View jyliang's full-sized avatar

Jason Liang jyliang

  • New York, NY
  • 23:38 (UTC -05:00)
View GitHub Profile
@jyliang
jyliang / UIImageCrop
Created March 4, 2014 20:23
UIImageCrop Utility
#define DEGREES_RADIANS(angle) ((angle) / 180.0 * M_PI)
- (UIImage*) UIImageCrop:(UIImage *)img withRect:(CGRect)rect {
CGAffineTransform rectTransform;
switch (img.imageOrientation)
{
case UIImageOrientationLeft:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(DEGREES_RADIANS(90)), 0, -img.size.height);
break;
case UIImageOrientationRight:
@jyliang
jyliang / ScrollViewCenter
Created March 5, 2014 01:08
ScrollView scroll to center point.
CGRect visibleRect = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
CGRect centeredRect = CGRectMake(visibleRect.origin.x + visibleRect.size.width/2.0 - self.scrollView.frame.size.width/2.0,
visibleRect.origin.y + visibleRect.size.height/2.0 - self.scrollView.frame.size.height/2.0,
self.scrollView.frame.size.width,
self.scrollView.frame.size.height);
[self.scrollView scrollRectToVisible:centeredRect animated:NO];
@jyliang
jyliang / method time
Last active August 29, 2015 13:57
method time
NSDate *start = [NSDate date];
//process
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:start];
NSLog(@"Execution Time: %f", executionTime);
@jyliang
jyliang / UIImage+FixOrientation
Created March 11, 2014 17:35
UIImage+FixOrientation
@interface UIImage (FixOrientation)
- (UIImage *)fixOrientation;
@end
@implementation UIImage (FixOrientation)
- (UIImage *)fixOrientation {
@jyliang
jyliang / vImageScaledImage
Created March 11, 2014 17:36
vImageScaledImage
// Method: vImageScaledImage:(UIImage*) sourceImage withSize:(CGSize) destSize
// Returns even better scaling than drawing to a context with kCGInterpolationHigh.
// This employs the vImage routines in Accelerate.framework.
// For more information about vImage, see https://developer.apple.com/library/mac/#documentation/performance/Conceptual/vImage/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001001-CH201-TPXREF101
// Large quantities of memory are manually allocated and (hopefully) freed here. Test your application for leaks before and after using this method.
+ (UIImage*) vImageScaledImage:(UIImage*) sourceImage withSize:(CGSize) destSize;
{
UIImage *destImage = nil;
if (sourceImage)
@jyliang
jyliang / quick nsoperation
Last active August 29, 2015 13:57
quick nsoperation
@implementation CustomOperation
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
@jyliang
jyliang / Transformation Normalize
Last active August 29, 2015 13:57
Transformation Normalize
- (CGFloat)getTransformScale {
CGAffineTransform transform = self.transform;
CGFloat scale = sqrt(transform.a*transform.a + transform.c*transform.c);
return scale;
}
- (CGFloat)getTransformAngle {
CGAffineTransform transform = self.transform;
CGFloat angle = atan2(transform.b, transform.a);
return angle;
@jyliang
jyliang / KVO
Last active April 13, 2016 14:36
KVO
static void * const MyClassKVOContext = (void*)&MyClassKVOContext;
- (void)addContext {
[object addObserver:self forKeyPath:NSStringFromSelector(@selector(property)) options:0 context:MyClassKVOContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == MyClassKVOContext) {
if ([keyPath isEqualToString:NSStringFromSelector(@selector(property))]) {
@jyliang
jyliang / unique ID
Created April 11, 2014 18:04
unique ID
+ (NSString *)createUniqueId {
CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef string = CFUUIDCreateString(kCFAllocatorDefault, cfuuid);
CFRelease(cfuuid);
return CFBridgingRelease(string);
}
@jyliang
jyliang / static inline
Created April 12, 2014 00:29
static inline
static inline NSString * ImageCacheKeyFromURLRequest(NSURLRequest *request) {
return [[request URL] absoluteString];
}