Skip to content

Instantly share code, notes, and snippets.

@CreatureSurvive
Last active January 25, 2018 03:27
Show Gist options
  • Save CreatureSurvive/39a2b3d3389ef6b47b1b8614648a5d5a to your computer and use it in GitHub Desktop.
Save CreatureSurvive/39a2b3d3389ef6b47b1b8614648a5d5a to your computer and use it in GitHub Desktop.
UIImage+squareImage
//
// UIImage+squareImage.h
//
// Created by CreatureSurvive on 1/24/18.
// Copyright © 2018 CreatureCoding. All rights reserved.
//
#import <UIKit/UIKit.h>
enum {
CropEdgeTop = 0,
CropEdgeBottom = 1,
CropEdgeCenter = 2
};
typedef NSUInteger CropEdge;
@interface UIImage (squareImage)
+ (UIImage *)squareImageFromImage:(UIImage *)image withCropEdge:(CropEdge)edge;
@end
//
// UIImage+squareImage.m
//
// Created by CreatureSurvive on 1/24/18.
// Copyright © 2018 CreatureCoding. All rights reserved.
//
#import "UIImage+squareImage.h"
@implementation UIImage (squareImage)
+ (UIImage *)squareImageFromImage:(UIImage *)image withCropEdge:(CropEdge)edge{
CGRect cropRect;
switch(edge) {
case CropEdgeBottom: {
cropRect = CGRectMake(0, image.size.height - image.size.width, image.size.width, image.size.width);
} break;
case CropEdgeTop: {
cropRect = CGRectMake(0, 0, image.size.width, image.size.width);
} break;
case CropEdgeCenter: {
cropRect = CGRectMake(0, (image.size.height / 2) - image.size.width / 2, image.size.width, image.size.width);
} break;
default: {
cropRect = CGRectMake(0, (image.size.height / 2) - image.size.width / 2, image.size.width, image.size.width);
} break;
}
return [[UIImage alloc] initWithCGImage:CGImageCreateWithImageInRect(image.CGImage, cropRect)];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment