Skip to content

Instantly share code, notes, and snippets.

@sdgandhi
sdgandhi / Save image to disk
Created May 21, 2015 23:11
iOS — Save image to documents directory
// Create path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:<#@"Image.png"#>];
// Save image.
[UIImage<#PNG#>Representation(image) writeToFile:filePath atomically:<#YES#>];
@sdgandhi
sdgandhi / UIView subclass XIB load
Created May 21, 2015 19:41
Setting up UIView subclass from XIB
@property (nonatomic, strong) UIView *view;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupViewFromNib];
[self setupUI];
}
return self;
@sdgandhi
sdgandhi / UICollectionViewFlowLayout arbitrary paging
Last active August 29, 2015 14:21
Paged UICollectionViewFlowLayout with snapping and centering for arbitrary items
- (CGSize)collectionViewContentSize
{
CGSize size = [super collectionViewContentSize];
CGSize cvSize = self.collectionView.frame.size;
CGSize newSize = CGSizeMake(size.width, size.height + ((cvSize.height/2) - (self.itemSize.height/2)));
return newSize;
}
// Vertical scrolling
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
@sdgandhi
sdgandhi / AVCaptureSession Example
Last active February 28, 2016 04:24
Setup camera capture and view on iOS (and flip camera + flashlight)
// *** Setup camera *** //
- (void)setupCamera
{
// setup session
self.session = [AVCaptureSession new];
self.session.sessionPreset = AVCaptureSessionPresetPhoto;
// setup device
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
@sdgandhi
sdgandhi / gist:6917822a68a01dba325c
Created March 18, 2015 18:56
iOS location permission denied
[UIAlertView bk_showAlertViewWithTitle:@"Location is disabled"
message:@"To enable location you need "
"to set location to 'Always' in Settings > <#app name#> > Location"
cancelButtonTitle:@"Cancel"
otherButtonTitles:@[ @"Settings" ]
handler:^(UIAlertView *alertView, NSInteger buttonIndex)
{
if (buttonIndex == 1) {
// Send the user to the Settings for this app
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
@sdgandhi
sdgandhi / Essential pods
Last active August 29, 2015 14:14
Essential pods
# Common
pod 'BlocksKit'
pod 'UIViewController+BlockSegue'
pod 'RKDropdownAlert'
pod 'UIAlertController+Blocks'
pod 'RFKeyboardToolbar'
pod 'SDWebImage'
pod 'TOWebViewController'
pod 'DZNEmptyDataSet'
@sdgandhi
sdgandhi / keyboard-shortcut-format.md
Last active August 29, 2015 14:14
Show keyboard shortcuts

To Run an XCode project: ⌘ + R

@sdgandhi
sdgandhi / iOS8NotificationActionsExample.m
Last active August 29, 2015 14:10
iOS 8 Notification Actions Example
// Borrowed some stuff from the WWDC 2014 video
// Create the action
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"ACCEPT_IDENTIFIER";
acceptAction.title = @"Accept";
acceptAction.activationMode = UIUserNotificationActivationModeBackground; // If we need to show UI, use UIUserNotificationActivationModeForeground
acceptAction.destructive = NO; // If YES, shows red button
acceptAction.authenticationRequired = NO; // If YES, requires passcode
#!/usr/bin/python
# Quick and dirty demonstration of CVE-2014-0160 by Jared Stafford ([email protected])
# The author disclaims copyright to this source code.
import sys
import struct
import socket
import time
import select