Last active
January 25, 2018 03:27
-
-
Save CreatureSurvive/39a2b3d3389ef6b47b1b8614648a5d5a to your computer and use it in GitHub Desktop.
UIImage+squareImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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