Skip to content

Instantly share code, notes, and snippets.

@phnessu4
Created May 27, 2015 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phnessu4/1475e9b9c4f6640ac236 to your computer and use it in GitHub Desktop.
Save phnessu4/1475e9b9c4f6640ac236 to your computer and use it in GitHub Desktop.
UIImage (Utility)
@interface UIImage (Utility)
+ (UIImage *)imageFromColor:(UIColor *)color;
-(UIImage *) getSubImage:(CGRect)rect;
-(UIImage *)scaleToSize:(CGSize)size;
+ (UIImage *)TransformtoSize:(CGSize)size image:(UIImage *)image;
- (UIImage *)imageWithMaxLength:(CGFloat)sideLenght;
@end
@interface UIImage (Tint)
- (UIImage *) imageWithTintColor:(UIColor *)tintColor;
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor;
@end
#import "UIImage+Utility.h"
@implementation UIImage (Utility)
+ (UIImage *)imageFromColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,[color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
// 截取部分图像
- (UIImage *)getSubImage:(CGRect)rect
{
CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
UIGraphicsBeginImageContext(smallBounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
UIImage *smallImage = [UIImage imageWithCGImage:subImageRef];
CFRelease(subImageRef);
UIGraphicsEndImageContext();
return smallImage;
}
// 等比例缩放
- (UIImage *)scaleToSize:(CGSize)size
{
CGFloat width = CGImageGetWidth(self.CGImage);
CGFloat height = CGImageGetHeight(self.CGImage);
float scale = [[UIScreen mainScreen] scale];
float verticalRadio = size.height*scale/height;
float horizontalRadio = size.width*scale/width;
float radio = 1;
if (verticalRadio > 1 && horizontalRadio > 1)
{
radio = 1;
}
else
{
radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
}
width = width * radio;
height = height * radio;
int xPos = (size.width - width/scale)/2;
int yPos = (size.height - height/scale)/2;
UIGraphicsBeginImageContextWithOptions(size, NO,scale);
[self drawInRect:CGRectMake(xPos, yPos, width/scale, height/scale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
+ (UIImage *)TransformtoSize:(CGSize)size image:(UIImage *)image
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *transformedImg=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return transformedImg;
}
- (UIImage *)imageWithMaxLength:(CGFloat)sideLenght
{
CGSize size = [self fitSize:sideLenght];
UIGraphicsBeginImageContext(size);
CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationDefault);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[self drawInRect:rect];
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
- (CGSize)fitSize:(CGFloat)sideLenght
{
CGFloat scale;
CGSize newsize;
if(self.size.width <= sideLenght && self.size.height <= sideLenght)
{
newsize = self.size;
}
else
{
if(self.size.width >= self.size.height)
{
scale = sideLenght/self.size.width;
newsize.width = sideLenght;
newsize.height = ceilf(self.size.height*scale);
}
else
{
scale = sideLenght/self.size.height;
newsize.height = sideLenght;
newsize.width = ceilf(self.size.width*scale);
}
}
return newsize;
}
@end
@implementation UIImage (Tint)
- (UIImage *) imageWithTintColor:(UIColor *)tintColor
{
return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];
}
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor
{
return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];
}
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode
{
//We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
//Draw the tinted image in context
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
if (blendMode != kCGBlendModeDestinationIn) {
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
}
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment