-
-
Save seivan/5924552 to your computer and use it in GitHub Desktop.
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+vImageScaling.h | |
// UIImage+vImageScaling | |
// | |
// Created by Matt Donnelly on 03/07/2013. | |
// Copyright (c) 2013 Matt Donnelly. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImage (vImageScaling) | |
- (UIImage *)vImageScaledImageWithSize:(CGSize)destSize; | |
- (UIImage *)vImageScaledImageWithSize:(CGSize)newSize contentMode:(UIViewContentMode)contentMode; | |
@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+vImageScaling.m | |
// UIImage+vImageScaling | |
// | |
// Created by Matt Donnelly on 03/07/2013. | |
// Copyright (c) 2013 Matt Donnelly. All rights reserved. | |
// | |
#import <Accelerate/Accelerate.h> | |
@implementation UIImage (vImageScaling) | |
- (UIImage *)vImageScaledImageWithSize:(CGSize)destSize contentMode:(UIViewContentMode)contentMode { | |
CGFloat horizontalRatio = destSize.width / self.size.width; | |
CGFloat verticalRatio = destSize.height / self.size.height; | |
CGFloat ratio; | |
switch (contentMode) | |
{ | |
case UIViewContentModeScaleAspectFill: | |
ratio = MAX(horizontalRatio, verticalRatio); | |
break; | |
case UIViewContentModeScaleAspectFit: | |
ratio = MIN(horizontalRatio, verticalRatio); | |
break; | |
default: | |
[NSException raise:NSInvalidArgumentException format:@"Unsupported content mode: %d", contentMode]; | |
} | |
CGSize newSize = CGSizeMake(round(self.size.width * ratio), round(self.size.height * ratio)); | |
return [self vImageScaledImageWithSize:newSize]; | |
} | |
- (UIImage *)vImageScaledImageWithSize:(CGSize)destSize { | |
UIImage *destImage = nil; | |
CGImageRef sourceRef = [self CGImage]; | |
NSUInteger sourceWidth = CGImageGetWidth(sourceRef); | |
NSUInteger sourceHeight = CGImageGetHeight(sourceRef); | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
unsigned char *sourceData = (unsigned char*) calloc(sourceHeight * sourceWidth * 4, sizeof(unsigned char)); | |
NSUInteger bytesPerPixel = 4; | |
NSUInteger sourceBytesPerRow = bytesPerPixel * sourceWidth; | |
NSUInteger bitsPerComponent = 8; | |
CGContextRef context = CGBitmapContextCreate(sourceData, sourceWidth, sourceHeight, | |
bitsPerComponent, sourceBytesPerRow, colorSpace, | |
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big); | |
CGContextDrawImage(context, CGRectMake(0, 0, sourceWidth, sourceHeight), sourceRef); | |
CGContextRelease(context); | |
NSUInteger destWidth = (NSUInteger) destSize.width; | |
NSUInteger destHeight = (NSUInteger) destSize.height; | |
NSUInteger destBytesPerRow = bytesPerPixel * destWidth; | |
unsigned char *destData = (unsigned char*) calloc(destHeight * destWidth * 4, sizeof(unsigned char)); | |
vImage_Buffer src = { | |
.data = sourceData, | |
.height = sourceHeight, | |
.width = sourceWidth, | |
.rowBytes = sourceBytesPerRow | |
}; | |
vImage_Buffer dest = { | |
.data = destData, | |
.height = destHeight, | |
.width = destWidth, | |
.rowBytes = destBytesPerRow | |
}; | |
vImage_Error err = vImageScale_ARGB8888(&src, &dest, NULL, kvImageHighQualityResampling); | |
free(sourceData); | |
CGContextRef destContext = CGBitmapContextCreate(destData, destWidth, destHeight, | |
bitsPerComponent, destBytesPerRow, colorSpace, | |
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big); | |
CGImageRef destRef = CGBitmapContextCreateImage(destContext); | |
destImage = [UIImage imageWithCGImage:destRef]; | |
CGImageRelease(destRef); | |
CGColorSpaceRelease(colorSpace); | |
CGContextRelease(destContext); | |
free(destData); | |
if (err != kvImageNoError) | |
{ | |
NSString *errorReason = [NSString stringWithFormat:@"vImageScale returned error code %zd", err]; | |
NSDictionary *errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
self, @"sourceImage", | |
[NSValue valueWithCGSize:destSize], @"destSize", | |
nil]; | |
NSException *exception = [NSException exceptionWithName:@"HighQualityImageScalingFailureException" reason:errorReason userInfo:errorInfo]; | |
@throw exception; | |
} | |
return destImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment