使用点阵图形上下文创建Image对象

来源:互联网 发布:js面向对象的理解 编辑:程序博客网 时间:2024/05/18 03:22

前言

大多情况下,我们绘图只是为了在屏幕上展示.但是有时候绘制一些离屏渲染是很有用的.比如,创建一个已存在图片的缩略图,将他写进缓存存成文件等等.为了满足这个需要,你创建一个使用点阵图形上下文,使用UIKit或者Core Craphics功能去绘制它,然后从上下文中获取这个image对象.

在UIKit库中,步骤如下:

调用UIGraphicsBeginImageContextWithOptions创建一个点阵图像上下文,然后压入图像栈中.

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
  • size表示上下文区域.
  • opaque 透明度
  • scale 尺寸因子 传0表示适配屏幕

    例如:一个200x200像素的区域.(200x200像素收到size和scale影响)

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100.0,100.0), NO, 2.0);

注意:避免使用类似的UICraphicsBeginImageContext功能,因为这总会创建一个scale=1.0的图像.如果当前设备是高分辨率屏,创建出的图片不会被渲染的很光滑.

通常使用UIKit或者Core Graphics去绘制图片内容到图像上下文中.

调用UIGraphicsGetImageFromCurrentImageContext方法,获取你当前创建的UIImage对象,如果需要的话,你还可以继续绘制,然后再调用此方法获取最新的图片.

调用UIGraphicsEndImageContext 退出graphics堆栈.

举个栗子:3-1获取一张下载好的图片,然后绘制到上下文中,缩放成appicon大小.然后从bitmap数据中获取一个UIimage对象,分配一个实例变量.需要注意的是创建的上下文中区域大小应该和图片大小一致.如果图片大于上下文中的区域,图片将会被裁减.

��3-1

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {    UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];    if (image != nil && image.size.width != kAppIconHeight && image.size.height != kAppIconHeight) {        CGRect imageRect = CGRectMake(0.0, 0.0, kAppIconHeight, kAppIconHeight);        UIGraphicsBeginImageContextWithOptions(itemSize, NO, [UIScreen mainScreen].scale);        [image drawInRect:imageRect];        self.appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext();  // UIImage returned.        UIGraphicsEndImageContext();    } else {        self.appRecord.appIcon = image;    }    self.activeDownload = nil;    [image release];    self.imageConnection = nil;    [delegate appImageDidLoad:self.indexPathInTableView];}

当然,你也可以使用Core Cgraphics功能来绘制图片内容.代码碎片如��3-2,绘制一个PDF图片,如下.注意:代码之前翻转图形上下文调用CGContextDrawPDFPage使绘制的图像与默认UIKit的坐标系统。
�� 3-2 用Core Graphics

// Other code precedes...CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);pdfScale = self.frame.size.width/pageRect.size.width;pageRect.size = CGSizeMake(pageRect.size.width * pdfScale, pageRect.size.height * pdfScale);UIGraphicsBeginImageContextWithOptions(pageRect.size, YES, pdfScale);CGContextRef context = UIGraphicsGetCurrentContext();// First fill the background with white.CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);CGContextFillRect(context,pageRect);CGContextSaveGState(context);// Flip the context so that the PDF page is rendered right side upCGContextTranslateCTM(context, 0.0, pageRect.size.height);CGContextScaleCTM(context, 1.0, -1.0);// Scale the context so that the PDF page is rendered at the// correct size for the zoom level.CGContextScaleCTM(context, pdfScale,pdfScale);CGContextDrawPDFPage(context, page);CGContextRestoreGState(context);UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];// Other code follows...

如果你更喜欢使用Core Graphics 来绘制图形上下文,你可以使用CGBitmapContextCreate来创建上下文,然后将image绘制进去.绘制结束,调用CGBitmapContextCreateImage来获取CGImageRef对象.你可以直接绘制Core Graphics image或者使用这个CGImageRef对象初始化一个UIImage对象.完成时,别人忘记调用CGContextRelease来释放上下文.