iOS开发UIImage+Addition

释放双眼,带上耳机,听听看~!

根据字符串生成二维码

声明.h

/**
*  根据字符串生成二维码
*
*  @param content 需要生成二维码的字符串
*
*  @return 生成二维码图片
*/
+ (UIImage *)qrImageWithContent:(NSString *)content;
/**
*  根据字符串和size生成二维码
*
*  @param content 需要生成二维码的字符串
*
*  @return 生成二维码图片
*/+ (UIImage *)qrImageWithContent:(NSString *)content size:(CGFloat)size;
/**
*  @param content 需要生成二维码的字符串
*  @param size    生成的二维码的清晰度(图片质量大小)100+
*  @param red,green,blue     生成的二维码的颜色
*  @return 生成二维码图片
*/
+ (UIImage *)qrImageWithContent:(NSString *)content size:(CGFloat)size red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue;
/**
*  @param content 需要生成二维码的字符串
*  @param logo    中间的logo图片
*  @param size    生成的二维码的清晰度(图片质量大小)100+
*  @param red,green,blue  生成的二维码的颜色
*  @return 生成二维码图片
*/
+ (UIImage *)qrImageWithContent:(NSString *)content logo:(UIImage *)logo size:(CGFloat)size red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue;

实现.m

void ProviderReleaseData (void *info, const void *data, size_t size){
free((void*)data);
}
+ (CIImage *)qrWithContent:(NSString *)content {
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
NSData *stringData = [content dataUsingEncoding:NSUTF8StringEncoding];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
CIImage *image = qrFilter.outputImage;
return image;
}
+ (UIImage *)qrImageWithContent:(NSString *)content size:(CGFloat)size  {
CIImage *image = [UIImage qrWithContent:content];
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 创建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
+ (UIImage *)qrImageWithContent:(NSString *)content size:(CGFloat)size red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue {
UIImage *image = [UIImage qrImageWithContent:content size:size];
const int imageWidth = image.size.width;
const int imageHeight = image.size.height;
size_t      bytesPerRow = imageWidth * 4;
uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,  kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
// 遍历像素
int pixelNum = imageWidth * imageHeight;
uint32_t* pCurPtr = rgbImageBuf;
for (int i = 0; i < pixelNum; i++, pCurPtr++){
if ((*pCurPtr & 0xFFFFFF00) < 0x99999900)    // 将白色变成透明
{
// 改成下面的代码,会将图片转成想要的颜色
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[3] = red; //0~255
ptr[2] = green;
ptr[1] = blue;
}
else
{
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[0] = 0;
}
}
// 输出图片
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);
CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace,
kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,
NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
// 释放内存空间
CGImageRelease(imageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return resultUIImage;
}
+ (UIImage *)qrImageWithContent:(NSString *)content logo:(UIImage *)logo size:(CGFloat)size red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue {
UIImage* resultUIImage = [self qrImageWithContent:content size:size red:red green:green blue:blue];
// 添加logo
CGFloat logoW = resultUIImage.size.width / 5.;
CGRect logoFrame = CGRectMake(logoW * 2, logoW * 2, logoW, logoW);
UIGraphicsBeginImageContext(resultUIImage.size);
[resultUIImage drawInRect:CGRectMake(0, 0, resultUIImage.size.width, resultUIImage.size.height)];
[logo drawInRect:logoFrame];
UIImage *kk = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return kk;
}
+ (UIImage *)qrImageWithContent:(NSString *)content {
NSData *stringData = [content dataUsingEncoding:NSUTF8StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
CIImage *qrImage = qrFilter.outputImage;
CGSize size = CGSizeMake(300, 300);
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextScaleCTM(context, 1, -1);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(cgImage);
return codeImage;
}

二维码

图片处理

声明.h

/**
*  根据给定的颜色生成 纯色图片
*
*  @param color 给定的颜色
*  @param size  生成图片大小
*
*  @return 生成图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
/**
*  把view转成图片
*
*  @param view 需要生成图片的view
*
*  @return 生成图片
*/
+ (UIImage *)imageForView:(UIView *)view;
/**
*  把view转成data
*
*  @param view 需要生成图片的view
*
*  @return 生成data
*/
+ (NSData *)imageToDataForView:(UIView *)view;
/**
*  把图片转换成圆形图片
*
*  @param imageName       图片名字
*  @param imageSizeOrZero 生成的图片尺寸
*
*  @return 生成图片
*/
+ (instancetype)circleImageWithName:(NSString *)imageName imageSize:(CGSize)imageSizeOrZero ;
/**
*  把图片转换成base64字符串
*
*  @param image 需要生成的图片
*
*  @return 生成的base64字符串
*/
+ (NSString *)base64EncodedStringByImage:(UIImage *)image;

实现.m

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
color.CGColor);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
+ (UIImage *)imageForView:(UIView *)view{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (NSData *)imageToDataForView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(image, 0);
}
+ (instancetype)circleImageWithName:(NSString *)imageName imageSize:(CGSize)imageSizeOrZero  {
//1. 加载原图片
UIImage *originalImage = [UIImage imageNamed:imageName];
CGSize size = !CGSizeEqualToSize(imageSizeOrZero, CGSizeZero) ? imageSizeOrZero : originalImage.size;
//2. 开启上下文
CGSize imageS = CGSizeMake(size.width, size.height);
UIGraphicsBeginImageContextWithOptions(imageS, NO, 2);
//3. 取得当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//4. 画圆
CGFloat bigRadius = size.width * 0.5;
CGContextAddArc(ctx, bigRadius, bigRadius, bigRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx);
CGContextClip(ctx);
//5. 画图
[originalImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
//6. 取图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+ (NSString *)base64EncodedStringByImage:(UIImage *)image {
NSData *data = UIImageJPEGRepresentation(image, 1);
NSString *iamgeData = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
return iamgeData;
}

人已赞赏
iOS文章

iOS JavaScript 与 Native交互(包括ReactNative)

2020-3-4 13:08:58

iOS文章

iOSTweak底层实现Cydia Substrate

2020-3-4 15:09:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索