Skip to content

Instantly share code, notes, and snippets.

@levey
Created October 2, 2012 02:17
Show Gist options
  • Save levey/3815788 to your computer and use it in GitHub Desktop.
Save levey/3815788 to your computer and use it in GitHub Desktop.
A way for UIImage's Anti-aliasing in Objective-C
//
// UIImage+Border.m
//
// Created by Levey on 11/10/11.
// Copyright (c) 2011 Vanillatech. All rights reserved.
//
#import "UIImage+Border.h"
static CGImageRef CreateMask(CGSize size, NSUInteger thickness)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
size.width,
size.height,
8,
size.width * 32,
colorSpace,
kCGBitmapByteOrderDefault | kCGImageAlphaNone);
if (bitmapContext == NULL)
{
NSLog(@"create mask bitmap context failed");
return nil;
}
// fill the black color in whole size, anything in black area will be transparent.
CGContextSetFillColorWithColor(bitmapContext, [UIColor blackColor].CGColor);
CGContextFillRect(bitmapContext, CGRectMake(0, 0, size.width, size.height));
// fill the white color in whole size, anything in white area will keep.
CGContextSetFillColorWithColor(bitmapContext, [UIColor whiteColor].CGColor);
CGContextFillRect(bitmapContext, CGRectMake(thickness, thickness, size.width - thickness * 2, size.height - thickness * 2));
// acquire the mask
CGImageRef maskImageRef = CGBitmapContextCreateImage(bitmapContext);
// clean up
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
return maskImageRef;
}
@implementation UIImage(Border)
- (UIImage *)imageWithColoredBorder:(NSUInteger)borderThickness borderColor:(UIColor *)color withShadow:(BOOL)withShadow
{
size_t shadowThickness = 0;
if (withShadow)
{
shadowThickness = 2;
}
size_t newWidth = self.size.width + 2 * borderThickness + 2 * shadowThickness;
size_t newHeight = self.size.height + 2 * borderThickness + 2 * shadowThickness;
CGRect imageRect = CGRectMake(borderThickness + shadowThickness, borderThickness + shadowThickness, self.size.width, self.size.height);
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetShadow(ctx, CGSizeZero, 4.5f);
[color setFill];
CGContextFillRect(ctx, CGRectMake(shadowThickness, shadowThickness, newWidth - 2 * shadowThickness, newHeight - 2 * shadowThickness));
CGContextRestoreGState(ctx);
[self drawInRect:imageRect];
//CGContextDrawImage(ctx, imageRect, self.CGImage); //if use this method, image will be filp vertically
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
return img;
}
- (UIImage *)imageWithTransparentBorder:(NSUInteger)thickness
{
size_t newWidth = self.size.width + 2 * thickness;
size_t newHeight = self.size.height + 2 * thickness;
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = bitsPerPixel * newWidth;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if(colorSpace == NULL)
{
NSLog(@"create color space failed");
return nil;
}
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
newWidth,
newHeight,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast);
if (bitmapContext == NULL)
{
NSLog(@"create bitmap context failed");
return nil;
}
// acquire image with opaque border
CGRect imageRect = CGRectMake(thickness, thickness, self.size.width, self.size.height);
CGContextDrawImage(bitmapContext, imageRect, self.CGImage);
CGImageRef opaqueBorderImageRef = CGBitmapContextCreateImage(bitmapContext);
// acquire image with transparent border
CGImageRef maskImageRef = CreateMask(CGSizeMake(newWidth, newHeight), thickness);
CGImageRef transparentBorderImageRef = CGImageCreateWithMask(opaqueBorderImageRef, maskImageRef);
UIImage *transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef];
// clean up
CGColorSpaceRelease(colorSpace);
CGContextRelease(bitmapContext);
CGImageRelease(opaqueBorderImageRef);
CGImageRelease(maskImageRef);
CGImageRelease(transparentBorderImageRef);
return transparentBorderImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment