IOS影像處理(7)繪製位元影像

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   for   

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)繪製位元影像

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.