首页 > 代码库 > IOS图像处理(7)绘制位图

IOS图像处理(7)绘制位图

UIImage提供了多种方法绘制位图,除了之前用过的drawInRect,我们还可以使用以下方法

drawAtPoint:需要提供CGPoint指定位图在CGContextRef中的起点

drawAtPoint:blendMode:alpha:后两个参数指定图片的叠加模式以及透明度

drawInRect:blendMode:alpha:后两个参数指定图片的叠加模式以及透明度

 

此外我们还可以借助Core Graphics中的方法绘制位图

void CGContextDrawImage(CGContextRef,CGRect,CGImageRef);

void CGContextDrawTiledImage(CGContextRef,CGRect,CGImageRef);此方法采用平铺模式将图片绘制到rect区域中

 

如果需要获得已有图片的全部或者部分,可以使用以下方法

CGImageRef CGImageCreateCopy(CGImageRef);

CGImageRef CGImageCreateWithImageInRect(CGImageRef,CGRect);

 

我们可以通过绘制位图给图片添加水印

@implementation ZLTView {    UIImage *_image;}- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        [self drawImage];            }    return self;}- (void)drawImage {    UIGraphicsBeginImageContext(self.frame.size);    CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSaveGState(context);        //Core Graphics中坐标系的坐标原点在屏幕左下角,正方形沿y轴向上,与UIKit相反,因此使用Core Graphic的函数直接绘制会得到y轴相反的图像    //首先y轴缩放-1,相当于沿着x张旋转180    CGContextScaleCTM(context, 1, -1);    //y轴进行平移,使原点移动到左上角    CGContextTranslateCTM(context, 0, -self.frame.size.height);    CGContextDrawImage(context, self.frame, [UIImage imageNamed:@"girl.jpg"].CGImage);        //回复之前的绘图环境    CGContextRestoreGState(context);        //使用UIKit进行绘图不需要变换举证,因为UIKit进行了处理    [@"made by zlt" drawAtPoint:CGPointMake(0, self.frame.size.height - 40) withAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont fontWithName:@"Arial" size:25]}];        _image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();}- (void)drawRect:(CGRect)rect{    [_image drawAtPoint:CGPointZero];}

 

下面例子为UIImage创建了一个分类,具体功能如下

+(UIImage *)captureView:(UIView *)targetView;      //获得UIView的截屏

+(UIImage *)captureScreent;                 //获得屏幕截屏 

-(UIImage *)imageAtRect:(CGRect)rect;           //获取已有的UIImage的一部分 

-(UIImage *)imageAspectByMinSize:(CGSize)targerSize;  //根据提供的区域短边对UIImage按比例缩放

-(UIImage *)imageAspectByMaxSize:(CGSize)targerSize;   //根据提供区域的长边对UIImage按比例缩放

-(UIImage *)imageRotate:(CGFloat)radians;         //对UIImage进行旋转

+(UIImage *)captureView:(UIView *)targetView {    UIGraphicsBeginImageContext(targetView.frame.size);    CGContextRef context = UIGraphicsGetCurrentContext();    [targetView.layer renderInContext:context];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}+(UIImage *)captureScreent {    //私有函数,也可以截取根UIView    extern CGImageRef UIGetScreenImage();    UIImage *image = [UIImage imageWithCGImage:UIGetScreenImage()];    return image;}-(UIImage *)imageAtRect:(CGRect)rect {    CGImageRef imgRef = CGImageCreateWithImageInRect([self CGImage], rect);    UIImage *image = [UIImage imageWithCGImage:imgRef];    return image;}-(UIImage *)imageAspectByMinSize:(CGSize)targerSize {        if (!CGSizeEqualToSize(self.size, targerSize)) {        CGFloat xfactor = targerSize.width / self.size.width;        CGFloat yfacotr = targerSize.height / self.size.height;        CGFloat factor = xfactor < yfacotr ? xfactor : yfacotr;                CGFloat nWidth = self.size.width * factor;        CGFloat nHeight = self.size.height * factor;                    CGRect rect;        if (xfactor < yfacotr) {                        rect = CGRectMake(0, (targerSize.height - nHeight) / 2, nWidth, nHeight);        } else {            rect = CGRectMake((targerSize.width - nWidth) / 2, 0, nWidth, nHeight);        }                        UIGraphicsBeginImageContext(targerSize);        [self drawInRect:rect];        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return image;            }        return self;}-(UIImage *)imageAspectByMaxSize:(CGSize)targerSize {        if (!CGSizeEqualToSize(self.size,targerSize)) {        CGFloat xfactor = targerSize.width / self.size.width;        CGFloat yfactor = targerSize.height / self.size.height;                CGFloat factor = xfactor > yfactor ? xfactor : yfactor;                CGFloat nWidth = self.size.width * factor;        CGFloat nHeight = self.size.height * factor;                        CGRect rect = CGRectZero;        if (xfactor > yfactor) {            rect = CGRectMake(0, -(nHeight - targerSize.height)/2, nWidth, nHeight);                    } else {            rect = CGRectMake(-(nWidth - targerSize.width)/2, 0, nWidth, nHeight);        }                UIGraphicsBeginImageContext(targerSize);        [self drawInRect:rect];        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return image;    }        return self;}-(UIImage *)imageRotate:(CGFloat)radians {        //获取旋转后的矩形区域    CGRect rect = CGRectApplyAffineTransform(CGRectMake(0, 0, self.size.width, self.size.height), CGAffineTransformMakeRotation(radians));        UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();        //将坐标中心移到图片中心,是图片绕中心旋转    CGContextTranslateCTM(context, rect.size.width/2, rect.size.height/2);    CGContextRotateCTM(context, radians);    CGContextScaleCTM(context, 1, -1);    CGContextDrawImage(context, CGRectMake(-rect.size.width/2, -rect.size.height/2, rect.size.width, rect.size.height), self.CGImage);        UIImage *nImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();                return nImage;}

 

IOS图像处理(7)绘制位图