zoukankan      html  css  js  c++  java
  • iOS基础之UIImageView(二)

    1 UIImage 任意角度旋转

    #import "UIImage+ImageRotate.h"
    #import <QuartzCore/QuartzCore.h>
    #import <Accelerate/Accelerate.h>
    
    @implementation UIImage (ImageRotate)
    
    /**
     UIImage 任意角度旋转
     1 将图片渲染到上下文 context
     2 将 context 旋转一定的角度
     3 将旋转之后的 context 转化为 UIImage
     */
    - (UIImage *)imageRaotateIndegree:(float)degree {
        // 1 将图片渲染到上下文 context
        size_t width = (size_t)self.size.width * self.scale;
        size_t height = (size_t)self.size.height * self.scale;
        
        // 每行图片数据字节
        size_t bytesPerRow = width * 4;
        // alpha 通道
        CGImageAlphaInfo alphaInfo = kCGImageAlphaPremultipliedFirst;
        // 配置上下文参数
        CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault | alphaInfo);
        if (!bmContext) {
            return nil;
        }
        CGContextDrawImage(bmContext, CGRectMake(0, 0, width, height), self.CGImage);
        // 2 将 context 旋转一定的角度
        UInt8 *data = (UInt8 *)CGBitmapContextGetData(bmContext);
        vImage_Buffer src = {data, height, width, bytesPerRow};
        vImage_Buffer dest = {data, height, width, bytesPerRow};
        Pixel_8888 bgColor = {0, 0, 0, 0};
        vImageRotate_ARGB8888(&src, &dest, NULL, degree, bgColor, kvImageBackgroundColorFill);
        // 3 将旋转之后的 context 转化为 UIImage
        CGImageRef rotateImageRef = CGBitmapContextCreateImage(bmContext);
        UIImage *rotateImage = [UIImage imageWithCGImage:rotateImageRef scale:self.scale orientation:UIImageOrientationUp];
        return rotateImage;
    }
    
    @end

    2 图片任意位置裁剪

    #import "UIImage+ImageCut.h"
    
    /**
     图片任意位置裁剪
     */
    - (UIImage *)imageCutSize:(CGRect)rect {
        // 1 将需要剪切的部分取出来
        CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
        CGRect smallRect = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
        // 2 将图片绘制到上下文中
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, rect, subImageRef);
        // 3 将当前上下文保存为UIImage
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    @end

    3 图片圆角剪切

    #import "UIImage+ImageCircle.h"
    
    @interface view: UIView
    
    @property (nonatomic, strong) UIImage *image;
    
    @end
    
    @implementation view
    
    - (void)drawRect:(CGRect)rect {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextAddEllipseInRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
        CGContextClip(context);
        CGContextFillPath(context);
        [_image drawAtPoint:CGPointMake(0, 0)];
        CGContextRestoreGState(context);
    }
    
    @end
    
    @implementation UIImage (ImageCircle)
    
    /**
     图片圆角剪切
     */
    - (UIImage *)imageClipCircle {
        CGFloat imageSizeMin = MIN(self.size.width, self.size.height);
        CGSize imageSize = CGSizeMake(imageSizeMin, imageSizeMin);
        
        view *mView = [[view alloc] init];
        mView.image = self;
        
        UIGraphicsBeginImageContext(imageSize);
        CGContextRef context = UIGraphicsGetCurrentContext();
        mView.frame = CGRectMake(0, 0, imageSizeMin, imageSizeMin);
        mView.backgroundColor = [UIColor whiteColor];
        [mView.layer renderInContext:context];
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    @end

    4 图片任意拉伸

    #import "UIImage+ImageScale.h"
    
    @implementation UIImage (ImageScale)
    
    /**
     图片任意拉伸
     */
    - (UIImage *)imageScaleSize:(CGSize)size {
        UIGraphicsBeginImageContext(size);
        [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    @end

    5 屏幕截屏

    #import "UIView+ImageScreenShot.h"
    
    @implementation UIView (ImageScreenShot)
    
    /**
     屏幕截屏
     */
    - (UIImage *)imageScreenShot {
        UIGraphicsBeginImageContext(self.frame.size);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.layer renderInContext:context];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        return image;
    }
    
    @end

    6 图片添加文字及logo水印效果

    #import "UIImage+ImageWaterPrint.h"
    
    @implementation UIImage (ImageWaterPrint)
    
    /**
     图片添加文字及logo水印效果
     */
    - (UIImage *)imageWater:(UIImage *)imageLogo waterString:(NSString *)waterString {
        UIGraphicsBeginImageContext(self.size);
        // 1 原始图片渲染
        [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
        
        // 2 将logo渲染到右下角
        [imageLogo drawInRect:CGRectMake(self.size.width - 20, self.size.height - 20, 16, 16)];
        
        // 3 将文字渲染到左上角
        NSMutableParagraphStyle *paragraphStype = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        paragraphStype.lineBreakMode = NSLineBreakByCharWrapping;
        NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:20],
                               NSForegroundColorAttributeName: [UIColor redColor],
                               NSParagraphStyleAttributeName: paragraphStype};
        [waterString drawInRect:CGRectMake(0, 0, 300, 50) withAttributes:dict];
        
        // 4 将当前上下文保存为UIImage
        UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        return imageNew;
    }
    
    @end
  • 相关阅读:
    SQL SERVER2005 的三种复制类型概述
    Autofac IoC容器基本实战【2】
    Autofac IoC容器基本使用步骤【1】
    在执行一行代码之前CLR做的68件事[The 68 things the CLR does before executing a single line of your code]
    .NET Entity Framework(EF)使用SqlQuery直接操作SQL查询语句或者执行过程
    使用 Entity Framework 7 进行 SQLite 的 CURD 操作
    NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
    Your Progress As A Programmer Is All Up To You
    为了效率而外包,不要因为懒惰外包
    10分钟看懂社群营销(上集)[转]
  • 原文地址:https://www.cnblogs.com/muzijie/p/9306379.html
Copyright © 2011-2022 走看看