Skip to content

Commit

Permalink
Added square cropping sample
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Jul 22, 2014
1 parent 9b4006d commit 8b35583
Show file tree
Hide file tree
Showing 11 changed files with 715 additions and 0 deletions.
451 changes: 451 additions & 0 deletions SquareCrop/SquareCrop.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions SquareCrop/SquareCrop/AppDelegate.h
@@ -0,0 +1,14 @@
//
// AppDelegate.h
// SquareCrop
//
// Created by Oliver Drobnik on 22.07.14.
// Copyright (c) 2014 Cocoanetics. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@end

22 changes: 22 additions & 0 deletions SquareCrop/SquareCrop/AppDelegate.m
@@ -0,0 +1,22 @@
//
// AppDelegate.m
// SquareCrop
//
// Created by Oliver Drobnik on 22.07.14.
// Copyright (c) 2014 Cocoanetics. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}

@end
39 changes: 39 additions & 0 deletions SquareCrop/SquareCrop/Base.lproj/Main.storyboard
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6185.7" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6181.2"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Uaa-Xr-RKf">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
</imageView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="Uaa-Xr-RKf" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" id="A4t-xg-v52"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="Uaa-Xr-RKf" secondAttribute="bottom" id="JOu-zM-mG3"/>
<constraint firstAttribute="trailing" secondItem="Uaa-Xr-RKf" secondAttribute="trailing" id="aiQ-WS-s5A"/>
<constraint firstItem="Uaa-Xr-RKf" firstAttribute="top" secondItem="kh9-bI-dsS" secondAttribute="top" id="i9P-ry-qrc"/>
</constraints>
</view>
<connections>
<outlet property="imageView" destination="Uaa-Xr-RKf" id="g59-9s-QqP"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
Binary file added SquareCrop/SquareCrop/Image.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
@@ -0,0 +1,23 @@
{
"images" : [
{
"orientation" : "portrait",
"idiom" : "iphone",
"extent" : "full-screen",
"minimum-system-version" : "7.0",
"scale" : "2x"
},
{
"orientation" : "portrait",
"idiom" : "iphone",
"subtype" : "retina4",
"extent" : "full-screen",
"minimum-system-version" : "7.0",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
32 changes: 32 additions & 0 deletions SquareCrop/SquareCrop/Info.plist
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.cocoanetics.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
</dict>
</plist>
16 changes: 16 additions & 0 deletions SquareCrop/SquareCrop/ViewController.h
@@ -0,0 +1,16 @@
//
// ViewController.h
// SquareCrop
//
// Created by Oliver Drobnik on 22.07.14.
// Copyright (c) 2014 Cocoanetics. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

79 changes: 79 additions & 0 deletions SquareCrop/SquareCrop/ViewController.m
@@ -0,0 +1,79 @@
//
// ViewController.m
// SquareCrop
//
// Created by Oliver Drobnik on 22.07.14.
// Copyright (c) 2014 Cocoanetics. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end


UIImage *squareCropImageToSideLength(UIImage *sourceImage,
CGFloat sideLength)
{
// input size comes from image
CGSize inputSize = sourceImage.size;

// round up side length to avoid fractional output size
sideLength = ceilf(sideLength);

// output size has sideLength for both dimensions
CGSize outputSize = CGSizeMake(sideLength, sideLength);

// calculate scale so that smaller dimension fits sideLength
CGFloat scale = MAX(sideLength / inputSize.width,
sideLength / inputSize.height);

// scaling the image with this scale results in this output size
CGSize scaledInputSize = CGSizeMake(inputSize.width * scale,
inputSize.height * scale);

// determine point in center of "canvas"
CGPoint center = CGPointMake(outputSize.width/2.0,
outputSize.height/2.0);

// calculate drawing rect relative to output Size
CGRect outputRect = CGRectMake(center.x - scaledInputSize.width/2.0,
center.y - scaledInputSize.height/2.0,
scaledInputSize.width,
scaledInputSize.height);

// begin a new bitmap context, scale 0 takes display scale
UIGraphicsBeginImageContextWithOptions(outputSize, YES, 0);

// optional: set the interpolation quality.
// For this you need to grab the underlying CGContext
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);

// draw the source image into the calculated rect
[sourceImage drawInRect:outputRect];

// create new image from bitmap context
UIImage *outImage = UIGraphicsGetImageFromCurrentImageContext();

// clean up
UIGraphicsEndImageContext();

// pass back new image
return outImage;
}


@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIImage *image = [UIImage imageNamed:@"Image.jpg"];
UIImage *scaled = squareCropImageToSideLength(image, 300);
self.imageView.image = scaled;
}

@end
16 changes: 16 additions & 0 deletions SquareCrop/SquareCrop/main.m
@@ -0,0 +1,16 @@
//
// main.m
// SquareCrop
//
// Created by Oliver Drobnik on 22.07.14.
// Copyright (c) 2014 Cocoanetics. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

0 comments on commit 8b35583

Please sign in to comment.