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

2 篇文章 0 订阅

前言

大多情况下,我们绘图只是为了在屏幕上展示.但是有时候绘制一些离屏渲染是很有用的.比如,创建一个已存在图片的缩略图,将他写进缓存存成文件等等.为了满足这个需要,你创建一个使用点阵图形上下文,使用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 up

CGContextTranslateCTM(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来释放上下文.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

严青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值