Skip to content

Instantly share code, notes, and snippets.

@nyoron
nyoron / gist:363423
Created April 12, 2010 10:12
Some sample code for a horizontal paged UIScrollView with a gap between each image, like Photos.app (which looks nicer when paging).
/*
Some sample code for a horizontal paged UIScrollView with a gap between each image (which looks nicer). Note - not using contentInset, we want each page to fill the iPhone screen and the gap only to be visible when scrolling (like Photos.app).
Taking the iPhone status bar into account - our viewport is 320x460
Our UIScrollView is therefore set in Interface Builder to 340x460 with a -10 X offset (so it's larger than the viewport but still centred)
Then we do the following...
*/
// Our image filenames
@ddewaele
ddewaele / gist:2479767
Last active May 24, 2016 05:56
Adding a title + subtitle to a navigation bar
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease];
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect titleFrame = CGRectMake(0, 2, 200, 24);
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = UITextAlignmentCenter;
static CGPathRef createClosedPathWithPoints(const CGPoint *points, size_t count) {
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddLines(path, NULL, points, count);
CGPathCloseSubpath(path);
return path;
}
static CGRect integralFrameForPath(CGPathRef path) {
CGRect frame = CGPathGetBoundingBox(path);
return CGRectIntegral(frame);
@justinHowlett
justinHowlett / gist:4611988
Last active July 24, 2018 06:13
Determine if a UIImage is generally dark or generally light
BOOL isDarkImage(UIImage* inputImage){
BOOL isDark = FALSE;
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(inputImage.CGImage));
const UInt8 *pixels = CFDataGetBytePtr(imageData);
int darkPixels = 0;
int length = CFDataGetLength(imageData);
@TimOliver
TimOliver / UIScrollView+ZoomToPoint.m
Created August 2, 2013 07:25
A small category for UIScrollView to programatically zoom to a CGPoint as opposed to a CGRect. Currently in use in my iOS app, iComics (http://icomics.co)
@implementation UIScrollView (ZoomToPoint)
- (void)zoomToPoint:(CGPoint)zoomPoint withScale: (CGFloat)scale animated: (BOOL)animated
{
//Normalize current content size back to content scale of 1.0f
CGSize contentSize;
contentSize.width = (self.contentSize.width / self.zoomScale);
contentSize.height = (self.contentSize.height / self.zoomScale);
//translate the zoom point to relative to the content rect
@mombrea
mombrea / iOS-UploadImage.h
Created January 17, 2014 01:49
example of a multi-part form post in objective-c
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"REST URL PATH"]];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"unique-consistent-string";
curl -O http://sketchtool.bohemiancoding.com/sketchtool-latest.zip
unzip sketchtool-latest.zip
sudo cp sketchtool/sketchtool /usr/bin/sketchtool
sudo cp -R sketchtool/sketchtool\ resources.bundle/ /usr/bin/sketchtool\ resources.bundle/
rm -r sketchtool/
rm sketchtool-latest.zip
@tempire
tempire / swift_draw_image_on_pdf.swift
Created April 25, 2015 09:24
swift draw image on pdf
func drawOnPDF(path: String) {
// Get existing Pdf reference
let pdf = CGPDFDocumentCreateWithURL(NSURL(fileURLWithPath: path))
// Get page count of pdf, so we can loop through pages and draw them accordingly
let pageCount = CGPDFDocumentGetNumberOfPages(pdf);
// Write to file
UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil)
@figgleforth
figgleforth / UIImage+pixelData.swift
Last active January 17, 2023 19:26
Extract RGBA pixel data from an UIImage in Swift
//
// UIImage+pixelData.swift
//
// Created by Bojan Percevic on 5/25/15.
// Copyright (c) 2015. All rights reserved.
//
import Foundation
extension UIImage {
@dhavaln
dhavaln / ViewController.swift
Created September 4, 2015 10:13
Swift / iOS - Extract Color from Image
func extractColor(image: UIImage){
let pixel = UnsafeMutablePointer<CUnsignedChar>.alloc(4)
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, bitmapInfo)
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image.CGImage)
var color: UIColor? = nil
if pixel[3] > 0 {