美文网首页
UIImage+Category常用分类方法

UIImage+Category常用分类方法

作者: 文子飞_ | 来源:发表于2020-12-14 16:05 被阅读0次
生成二维码
@implementation UIImage (createImage)
// 获取截屏生成image
+ (UIImage *)getScreenShot:(CGRect)rect ofView:(UIView *)view {
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);

    if (@available(iOS 9.0, *)) {
        
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        
    } else if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        
        BOOL isComplate = [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
        NSLog(@">>>>>>>>>>>>>>>>%@", isComplate?@"YES":@"NO");
    } else {
        
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

// 生成二维码
+ (UIImage *)createQRCodeImageWithString:(NSString *)qrString {
    @autoreleasepool {
        // Need to convert the string to a UTF-8 encoded NSData object
        NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
        // Create the filter
        CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        // Set the message content and error-correction level
        [qrFilter setValue:stringData forKey:@"inputMessage"];
        [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
        // Send the image back
        CIImage* image = qrFilter.outputImage;
        
        CGFloat size = 250.0f;
        CGRect extent = CGRectIntegral(image.extent);
        CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
        // create a bitmap image that we'll draw into a bitmap context at the desired size;
        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);
        // Create an image with the contents of our bitmap
        CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
        UIImage *resultImage = [UIImage imageWithCGImage:scaledImage];
        // Cleanup
        CGContextRelease(bitmapRef);
        CGImageRelease(bitmapImage);
        CGColorSpaceRelease(cs);
        CGImageRelease(scaledImage);
        return resultImage;
    }
}

// 将颜色生成图片
+ (UIImage *)createImageWithColor:(UIColor*)color{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
修改图标颜色
@implementation UIImage (RenderColor)

- (UIImage*)imageChangeColor:(UIColor*)color {
    //获取画布
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    //画笔沾取颜色
    [color setFill];
    
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    UIRectFill(bounds);
    //绘制一次
    [self drawInRect:bounds blendMode:kCGBlendModeOverlay alpha:1.0f];
    //再绘制一次
    [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    //获取图片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

@end

相关文章

  • UIImage+Category常用分类方法

    生成二维码 修改图标颜色

  • iOS开发中常用的分类方法---UIImage+Category

    在开发中使用分类对原有的系统类进行方法扩展,是增强系统原有类功能的常见做法。

  • k近邻分类器

      K近邻分类器是机器识别中很常用的一种分类方法,以前在做单样本人脸识别的时候常用的最近邻分类方法就是其中k=1的...

  • HTTP报文

    报文 请求报文 响应报文 备注: HTTP常用方法 GET方法与POST方法的区别 状态码分类状态码分类.png ...

  • 统计学习方法 | 朴素贝叶斯法

    01 分类方法 之前我们学习了一种分类方法——K近邻法(KNN),今天我们再学习一种更常用的分类方法 朴素贝叶斯法...

  • 跟骨骨折分型复习

    目前跟骨骨折常用分类方法以Sanders分型为主,当然Essex-Lopresti的关节压缩型和舌型分类也常用。 ...

  • iOS常用分类方法

    在工作中总结了一些开发中常用到的分类,使用这些分类能使开发效率更高,让代码变得更简洁,减少了很多不必要的代码量,直...

  • 基于深度学习的视频检测分类综述(1)

    视频检测分类常用于视频分类,目标行为检测。本博客详细的介绍目前常用的基于深度学习的视频检测分类方法,分别从不同的方...

  • 数组去重

    分类 非对象数组去重 对象数组去重 分类一 --- 非对象数组去重 方法一: set(es6常用) 方法二:red...

  • iOS 截屏的方法,拉伸图片的方法,压缩图片的方法

    本次更新三个常用的方法,全部写成了 UIImage 的分类方法 截屏 压缩图片 拉伸图片

网友评论

      本文标题:UIImage+Category常用分类方法

      本文链接:https://www.haomeiwen.com/subject/npwvgktx.html