gpt4 book ai didi

iphone - 如何使用 UIGraphicsBeginImageContextWithOptions?

转载 作者:行者123 更新时间:2023-12-03 18:43:10 27 4
gpt4 key购买 nike

我正在努力适应THIS问题与视网膜显示器一起使用。以下是我如何弄清楚如何使用 UIGraphicsBeginImageContext 的视网膜图形。

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

但是,当我使用它时,图像看起来非常大,并且无法缩小以适应显示屏。我有什么想法可以告诉它调整捕获的图像大小以适合盒子吗? (请参阅其他问题以理解我的意思)。

最佳答案

我编写此代码的目的是执行另一篇文章中描述的完全相同的操作。它适用于任何至少装有 iOS 3.1 的 iOS 设备。

_launcherView为需要拍照的view

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);

关于iphone - 如何使用 UIGraphicsBeginImageContextWithOptions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5146008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com