如何使用UIGraphicsBeginImageContextWithOptions? [英] How to use UIGraphicsBeginImageContextWithOptions?

查看:181
本文介绍了如何使用UIGraphicsBeginImageContextWithOptions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试适应这个问题与视网膜显示器一起工作。这是我如何弄清楚如何使用视网膜图形 UIGraphicsBeginImageContext

I'm trying to adapt THIS question to work with a retina display. Here's how I figured out how to work with retina graphics for UIGraphicsBeginImageContext.

if (UIGraphicsBeginImageContextWithOptions != NULL)
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0f);
}
else
{
    UIGraphicsBeginImageContext(self.view.bounds.size);
}

但是,当我使用它时,图像看起来真的很大,下来适合显示。任何想法如何我可以告诉它调整捕获的图像大小适合框? (请参考另一个问题来理解我的意思)。

However, when I use it the image looks really huge and doesn't scale down to fit the display. Any ideas how I can tell it to resize the captured image to fit the boxes? (Please refer to the other question to understand what I mean).

推荐答案

我写了这个代码来执行完全一样在另一篇帖子中描述的东西。
它可以在任何iOS设备至少iOS 3.1。

I've written this code to perform exactly the same thing described in the other post. It works on any iOS device with at least iOS 3.1.

_launcherView是需要拍摄的视图

_launcherView is the view that need to be photographed

CGFloat scale = 1.0;
if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {        
    CGFloat tmp = [[UIScreen mainScreen]scale];
    if (tmp > 1.5) {
        scale = 2.0;    
    }
} 

if(scale > 1.5) {
    UIGraphicsBeginImageContextWithOptions(_launcherView.frame.size, NO, scale);
} else {
    UIGraphicsBeginImageContext(_launcherView.frame.size);
}

[_launcherView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGRect upRect = CGRectMake(0, 0, _launcherView.frame.size.width*scale, (diff - offset)*scale);
CGImageRef imageRefUp = CGImageCreateWithImageInRect([screenshot CGImage], upRect);
[self.screenshot1 setFrame:CGRectMake(0, 0, screenshot1.frame.size.width, diff - offset)];
[screenshot1 setContentMode:UIViewContentModeTop];
UIImage * img1 = [UIImage imageWithCGImage:imageRefUp];
[self.screenshot1 setBackgroundImage:img1 forState:UIControlStateNormal];
CGImageRelease(imageRefUp);

CGRect downRect = CGRectMake(0, (diff - offset)*scale, _launcherView.frame.size.width*scale, (screenshot.size.height - diff + offset)*scale);
CGImageRef imageRefDown = CGImageCreateWithImageInRect([screenshot CGImage], downRect);
[self.screenshot2 setFrame:CGRectMake(0, screenshot1.frame.size.height ,  screenshot2.frame.size.width, _launcherView.frame.size.height - screenshot1.frame.size.height)];
[screenshot2 setContentMode:UIViewContentModeTop];
UIImage * img2 = [UIImage imageWithCGImage:imageRefDown];
[self.screenshot2 setBackgroundImage:img2 forState:UIControlStateNormal];
CGImageRelease(imageRefDown);

这篇关于如何使用UIGraphicsBeginImageContextWithOptions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆