Skip to content

Instantly share code, notes, and snippets.

@luowei
Created November 21, 2016 03:54
Show Gist options
  • Save luowei/a7b2464d6cab805591c935c2579b5043 to your computer and use it in GitHub Desktop.
Save luowei/a7b2464d6cab805591c935c2579b5043 to your computer and use it in GitHub Desktop.
UIImage OC扩展
@interface UIImage(Color)
/**
* 给指定的图片染色
*/
- (UIImage *)imageWithOverlayColor:(UIColor *)color;
//根据颜色与矩形区生成一张图片
+ (UIImage *)imageFromColor:(UIColor *)color withRect:(CGRect)rect;
//根据View获得一张图片
+ (UIImage *)imageWithView:(UIView *)view;
//把字符串依据指定的字体属性及大小转换成图片
+ (UIImage *)imageFromString:(NSString *)string attributes:(NSDictionary *)attributes size:(CGSize)size;
@end
@interface UIImage(Cut)
//根据指定矩形区,剪裁图片
- (UIImage *)cutImageWithRect:(CGRect)cutRect;
//在指定大小的绘图区域内,将img2合成到img1上
+ (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2
andRect:(CGRect)cropRect withImageSize:(CGSize)size;
//把一张图片缩放到指定大小
- (UIImage *)imageToscaledSize:(CGSize)newSize;
//把一张图片按比例缩放到指定大小
- (UIImage*)scaleToSizeKeepAspect:(CGSize)size;
//把图片按指定比例缩放
- (UIImage *)imageToScale:(CGFloat)scale;
@end
@interface UIImage (AverageColor)
//获得图片的平均色值
- (UIColor *)averageColor;
@end
@interface UIImage(Alpha)
- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha;
@end
//------- Implementation -------
@implementation UIImage (Color)
/**
* 给指定的图片染色
*/
- (UIImage *)imageWithOverlayColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
// if (UIGraphicsBeginImageContextWithOptions) {
CGFloat imageScale = 1.0f;
if ([self respondsToSelector:@selector(scale)]) // The scale property is new with iOS4.
imageScale = self.scale;
UIGraphicsBeginImageContextWithOptions(self.size, NO, imageScale);
// }
// else {
// UIGraphicsBeginImageContext(self.size);
// }
[self drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//根据颜色与矩形区生成一张图片
+ (UIImage *)imageFromColor:(UIColor *)color withRect:(CGRect)rect {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//根据View获得一张图片
+ (UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
//把字符串依据指定的字体属性及大小转换成图片
+ (UIImage *)imageFromString:(NSString *)string attributes:(NSDictionary *)attributes size:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[string drawInRect:CGRectMake(0, 0, size.width, size.height) withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
@implementation UIImage (Cut)
//根据指定矩形区,剪裁图片
- (UIImage *)cutImageWithRect:(CGRect)cutRect {
CGImageRef cutImageRef = CGImageCreateWithImageInRect(self.CGImage, cutRect);
UIImage *cutImage = [UIImage imageWithCGImage:cutImageRef];
return cutImage;
}
//在指定大小的绘图区域内,将img2合成到img1上
+ (UIImage *)addImageToImage:(UIImage *)img withImage2:(UIImage *)img2
andRect:(CGRect)cropRect withImageSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGPoint pointImg1 = CGPointMake(0, 0);
[img drawAtPoint:pointImg1];
CGPoint pointImg2 = cropRect.origin;
[img2 drawAtPoint:pointImg2];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
//把一张图片缩放到指定大小
- (UIImage *)imageToscaledSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//把一张图片按长宽等比例缩放到适应指定大小
- (UIImage *)scaleToSizeKeepAspect:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGFloat ws = size.width / self.size.width;
CGFloat hs = size.height / self.size.height;
if (ws > hs) {
ws = hs / ws;
hs = 1.0;
} else {
hs = ws / hs;
ws = 1.0;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(size.width / 2 - (size.width * ws) / 2,
size.height / 2 - (size.height * hs) / 2, size.width * ws,
size.height * hs), self.CGImage);
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
//把图片按指定比例缩放
- (UIImage *)imageToScale:(CGFloat)scale {
UIGraphicsBeginImageContextWithOptions(self.size, YES, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
@implementation UIImage (AverageColor)
//获得图片的平均色值
- (UIColor *)averageColor {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char rgba[4];
CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
if(rgba[3] > 0) {
CGFloat alpha = ((CGFloat)rgba[3])/255.0;
CGFloat multiplier = alpha/255.0;
return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier
green:((CGFloat)rgba[1])*multiplier
blue:((CGFloat)rgba[2])*multiplier
alpha:alpha];
}
else {
return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0
green:((CGFloat)rgba[1])/255.0
blue:((CGFloat)rgba[2])/255.0
alpha:((CGFloat)rgba[3])/255.0];
}
}
@end
@implementation UIImage(Alpha)
//修改图片透明度
- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, self.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment