因为涉及到图片, 运用到了主流的三方库SDWebImage
.h文件
#import <UIKit/UIKit.h>
@interface UIImage (MaxMethod)
// 压缩图片尺寸
- (UIImage *)setImagesSize:(CGSize)size;
- (UIImage *) clipImageWithMaxBorder:(CGFloat)max;
/**
* 添加水印,分宽高两种
*
* @param logo 要添加的LOGO图案
* @param scale LOG图案沾总图的宽或者高的比例0.0~1.0
*
* @return 新生成的图片
*/
- (UIImage *)addImageLogo:(UIImage *)logo withScale:(CGFloat)scale;
/**
* 添加水印,按宽的比例
*
* @param logo 要添加的LOGO图案
* @param scale LOG图案沾总图的宽的比例0.0~1.0
*
* @return 新生成的图片
*/
- (UIImage *)addImageLogo:(UIImage *)logo withWidthScale:(CGFloat)scale;
// 生成一个渐变图片
+ (UIImage *)createImage:(NSArray *)colors locations:(NSArray *)locations from:(CGPoint)from to:(CGPoint)to;
// 加载Gif图
+ (UIImage *)animationWithGifData:(NSData *)data;
/**
创建1*1大小的纯色图片
@param color 图片
颜色
@return 创建的图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color;
/**
创建指定大小、颜色的图片
@param color 图片颜色
@param size 图片大小
@return 创建的图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
/**
创建纯色背景、文字居中的图片
@param color 图片背景色
@param size 图片大小
@param attributeString 居中文字
@return 创建的图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size text:(NSAttributedString *)attributeString;
/**
裁剪圆形图片
@param image 需要裁剪的图片
@param inset 裁剪inset
@return 裁剪后的图片
*/
+ (UIImage *)ellipseImage:(UIImage *)image withInset:(CGFloat)inset;
/**
裁剪带边框的圆形图片
@param image 需要裁剪的图片
@param inset 裁剪inset
@param width 边框宽度
@param color 边框颜色
@return 裁剪后的图片
*/
+ (UIImage *)ellipseImage: (UIImage *) image withInset:(CGFloat)inset withBorderWidth:(CGFloat)width withBorderColor:(UIColor*)color;
@end
.m文件
#import "UIImage+MaxMethod.h"
#import <SDWebImage/UIImage+GIF.h>
@implementation UIImage (MaxMethod)
- (UIImage *)setImagesSize:(CGSize)size
{
UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)clipImageWithMaxBorder:(CGFloat)max {
UIImage *image = nil;
CGFloat width = 0.0;
CGFloat height = 0.0;
if (self.size.width > self.size.height) {
width = max;
height = self.size.height * (max / self.size.width);
} else {
height = max;
width = self.size.width * (max / self.size.height);
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 1.0);
[self drawInRect:CGRectMake(0, 0, width, height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)addImageLogo:(UIImage *)logo withScale:(CGFloat)scale
{
UIImage *image = nil;
// NSLogSize(self.size);
UIGraphicsBeginImageContextWithOptions(self.size, YES, 0.0);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
CGFloat logoW,logoH;
if (0 != scale) {
CGFloat proportion = logo.size.height / logo.size.width;
if (self.size.width > self.size.height) {
logoW = self.size.width * scale;
logoH = logoW * proportion;
} else {
logoH = self.size.height * scale;
logoW = logoH / proportion;
}
} else {
logoW = logo.size.width;
logoH = logo.size.height;
}
CGFloat logoX = self.size.width - logoW;
CGFloat logoY = self.size.height - logoH;
[logo drawInRect:CGRectMake(logoX, logoY, logoW, logoH)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)addImageLogo:(UIImage *)logo withWidthScale:(CGFloat)scale
{
UIImage *image = nil;
// NSLogSize(self.size);
UIGraphicsBeginImageContextWithOptions(self.size, YES, 0.0);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
CGFloat logoW,logoH;
CGFloat proportion = logo.size.height / logo.size.width;
logoW = self.size.width * scale;
logoH = logoW * proportion;
CGFloat logoX = self.size.width - logoW;
CGFloat logoY = self.size.height - logoH;
[logo drawInRect:CGRectMake(logoX, logoY, logoW, logoH)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)createImage:(NSArray *)colors locations:(NSArray *)locations from:(CGPoint)from to:(CGPoint)to {
CGSize size = CGSizeMake(colors.count, colors.count);
CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = CGRectMake(0, 0, size.width, size.height);
layer.colors = colors;
layer.locations = locations;
layer.startPoint = from;
layer.endPoint = to;
UIGraphicsBeginImageContext(size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)createShadowImage {
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 1, 1)];
[[UIColor clearColor] set];
[path fill];
UIImage *shadowImage = UIGraphicsGetImageFromCurrentImageContext();
return shadowImage;
}
+ (UIImage *)animationWithGifData:(NSData *)data {
if (!data) {
return nil;
}
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(source);
UIImage *animatedImage;
if (count <= 1) {
animatedImage = [[UIImage alloc] initWithData:data];
} else {
CGFloat scale = 1;
scale = [UIScreen mainScreen].scale;
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < count; i++) {
CGImageRef CGImage = CGImageSourceCreateImageAtIndex(source, i, NULL);
UIImage *frameImage = [UIImage imageWithCGImage:CGImage scale:scale orientation:UIImageOrientationUp];
[images addObject:frameImage];
CGImageRelease(CGImage);
}
animatedImage = [UIImage animatedImageWithImages:images duration:images.count * 0.1];
}
CFRelease(source);
return animatedImage;
}
+ (UIImage *)imageWithColor:(UIColor *)color {
return [UIImage imageWithColor:color size:CGSizeMake(1.f, 1.f)];
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size text:(NSAttributedString *)attributeString {
UIImage *image = [UIImage imageWithColor:color size:size];
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
CGSize textSize = [attributeString size];
CGFloat textX = (size.width - textSize.width) / 2.f;
CGFloat textY = (size.height - textSize.height) / 2.f;
CGRect textRect = CGRectMake(textX, textY, textSize.width,textSize.height);
UIGraphicsBeginImageContext(size);
[image drawInRect:rect];
[attributeString drawInRect:textRect];
UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+ (UIImage *)ellipseImage:(UIImage *)image withInset:(CGFloat)inset {
return [self ellipseImage:image withInset:inset withBorderWidth:0 withBorderColor:[UIColor clearColor]];
}
+ (UIImage *)ellipseImage: (UIImage *) image withInset:(CGFloat)inset withBorderWidth:(CGFloat)width withBorderColor:(UIColor*)color {
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.f , image.size.height - inset * 2.f);
CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[image drawInRect:rect];
if (width > 0) {
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextSetLineCap(context,kCGLineCapButt);
CGContextSetLineWidth(context, width);
CGRect rect2 = CGRectMake(inset + width/2, inset + width/2, image.size.width - width- inset * 2.0f, image.size.height - width- inset * 2.0f);
CGContextAddEllipseInRect(context, rect2);
CGContextStrokePath(context);
}
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
@end
网友评论