Skip to content

Instantly share code, notes, and snippets.

@YGeorge
YGeorge / Facebook.mm
Last active December 17, 2015 16:58
Facebook for iOS 5 and 6
- (IBAction)fbBtnTouchUp:(id)sender {
if(NSClassFromString(@"SLComposeViewController") != nil) {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:self.post.title];
[controller addURL:[NSURL URLWithString:self.post.url]];
[self presentViewController:controller animated:YES completion:Nil];
@YGeorge
YGeorge / gist:8053506
Created December 20, 2013 11:22
iOS scale image proportionally DEFAULT_PHOTO_MAX_SIZE - max size (height or width) of new image
+ (UIImage *)scaleImageProportionally:(UIImage *)image {
if (MAX(image.size.height, image.size.width) <= DEFAULT_PHOTO_MAX_SIZE) {
return image;
}
else {
CGFloat targetWidth = 0;
CGFloat targetHeight = 0;
if (image.size.height > image.size.width) {
CGFloat ratio = image.size.height / image.size.width;
- (void)setPhotoByURLString:(NSString *)URLString {
if (URLString) {
URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache queryDiskCacheForKey:URLString done:^(UIImage *image, SDImageCacheType cacheType)
{
if (image) {
NSLog(@"image not null!!");
[self.photoView setImage:image];
}
Replace your enum declarations, such as this one:
enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
};
typedef NSInteger UITableViewCellStyle;
with the NS_ENUM syntax:
ChatMessage *message = [chatMessages objectAtIndex:indexPath.row];
CGFloat topMargin = (indexPath.row == 0) ? 18 : 0;
CGRect stringRect = [message.text boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName : [UIFont fontWithName:CHAT_CELL_FONT size:14] }
context:nil];
CGSize stringSize = CGRectIntegral(stringRect).size;
CGFloat bottomMargin = [self isMessageHasTail:indexPath.row] ? 30 : 20;
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
@YGeorge
YGeorge / recursiveDescription
Created April 28, 2014 09:22
for examining your view hierarchy
#ifdef DEBUG
NSLog(@"Cell recursive description:\n\n%@\n\n", [cell performSelector:@selector(recursiveDescription)]);
#endif
@YGeorge
YGeorge / UITextView placeholder
Last active August 31, 2015 13:25
UITextView placeholder
- (void)textViewDidEndEditing:(UITextView *)txtView {
self.placeholderLabel.hidden = ([txtView.text length] > 0);
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
self.placeholderLabel.hidden = YES;
return YES;
}
@YGeorge
YGeorge / scaleImageToResolution
Created June 5, 2014 11:34
+ (UIImage *)scaleImage:(UIImage*)image toResolution:(int)resolution {
+ (UIImage *)scaleImage:(UIImage*)image toResolution:(int)resolution {
CGImageRef imgRef = [image CGImage];
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
//if already at the minimum resolution, return the orginal image, otherwise scale
if (width <= resolution && height <= resolution) {
return image;
@YGeorge
YGeorge / StrongAndWeak
Created July 22, 2014 05:27
Obj-C Strong and Weak references
Представьте, что наш объект — это собака. Собака хочет «убежать» (освободить память).
Сильный указатель — это поводок с ошейником. Пока поводок прицеплен к ошейнику, собака не убежит.
Если 5 человек прицепят 5 поводков к одному ошейнику (5 указателей на 1 объект) — собака не убежит до тех пор,
пока не отцепят все 5 поводков.
А слабые указатели — это дети, которые тычут пальцем на собаку и кричат: «Ух ты, собака!»
Пока собака на поводке, они могут тыкать («указывать на объект») сколько угодно.
Но если отсоединить все поводки, то собака убежит, независимо от того, сколько детей тычут в неё пальцем.