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