gpt4 book ai didi

objective-c - 缩放包含UIView时,UIImage/UIImageView重绘

转载 作者:行者123 更新时间:2023-12-01 15:56:36 26 4
gpt4 key购买 nike

我的iPad应用程序有一个导航,其中我显示不同页面的屏幕截图,并且由于我想一次显示多个屏幕截图,因此我将容器缩放到原始屏幕截图(1024x768)的大约24%。

- (void) loadView
{
// get landscape screen frame
CGRect screenFrame = [UIScreen mainScreen].bounds;
CGRect landscapeFrame = CGRectMake(0, 0, screenFrame.size.height, screenFrame.size.width);

UIView *view = [[UIView alloc] initWithFrame:landscapeFrame];
view.backgroundColor = [UIColor grayColor];

self.view = view;

// add container view for 2 images
CGRect startFrame = CGRectMake(-landscapeFrame.size.width/2, 0, landscapeFrame.size.width*2, landscapeFrame.size.height);
container = [[UIView alloc] initWithFrame:startFrame];
container.backgroundColor = [UIColor whiteColor];

// add image 1 (1024x768)
UIImage *img1 = [UIImage imageNamed:@"01.jpeg"];
UIImageView *img1View = [[UIImageView alloc] initWithImage:img1];
[container addSubview:img1View];

// add image 2 (1024x768)
UIImage *img2 = [UIImage imageNamed:@"02.jpeg"];
UIImageView *img2View = [[UIImageView alloc] initWithImage:img2];

// move img2 to the right of img1
CGRect newFrame = img2View.frame;
newFrame.origin.x = 1024.0;
img2View.frame = newFrame;

[container addSubview:img2View];

// scale to 24%
container.transform = CGAffineTransformMakeScale(0.24, 0.24);

[self.view addSubview:container];
}

但是当我用“小”文字缩放图像时,它看起来像这样:

我必须使用大屏幕截图,因为如果用户点击图像,则图像应缩放至100%且清晰可见。

有没有一种方法可以在不影响性能的情况下(平滑)缩放图像(动态)?
拥有两个版本就足够了:一个为全像素版本,另一个为24%版本。

最佳答案

缩小后的图像显得cr脚的原因是它在OpenGL中进行了缩放,而OpenGL使用的是快速但低质量的线性插值。您可能知道,UIView构建在CALayer之上,而CALayer又是OpenGL纹理的一种包装。由于该层的内容驻留在视频卡中,因此CALayer可以在GPU上发挥其所有魔力,而与CPU是否忙于加载网站,是否阻止磁盘访问等无关。我之所以仅提及这一点,是因为注意图层内部纹理中的实际内容很有用。在您的情况下,UIImageView的图层在其纹理上具有完整的1024x768位图图像,并且不受容器的转换的影响:UIImageView内的CALayer不会看到它会变成(请看一下。)246x185 -screen并重新缩放其位图,它只允许OpenGL执行其操作并在每次更新显示时缩小位图。

为了获得更好的缩放比例,我们需要在CoreGraphics中而不是在OpenGL中进行缩放。这是一种实现方法:

- (UIImage*)scaleImage:(UIImage*)image by:(float)scale
{
CGSize size = CGSizeMake(image.size.width * scale, image.size.height * scale);
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return imageCopy;
}

- (void)loadView
{
// get landscape screen frame
CGRect screenFrame = [UIScreen mainScreen].bounds;
CGRect landscapeFrame = CGRectMake(0, 0, screenFrame.size.height, screenFrame.size.width);

UIView *view = [[UIView alloc] initWithFrame:landscapeFrame];
view.backgroundColor = [UIColor grayColor];

self.view = view;

// add container view for 2 images
CGRect startFrame = CGRectMake(-landscapeFrame.size.width/2, 0, landscapeFrame.size.width*2, landscapeFrame.size.height);
container = [[UIView alloc] initWithFrame:startFrame];
container.backgroundColor = [UIColor whiteColor];

// add image 1 (1024x768)
UIImage *img1 = [UIImage imageNamed:@"01.png"];
img1View = [[TapImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
img1View.userInteractionEnabled = YES; // important!
img1View.image = [self scaleImage:img1 by:0.24];
[container addSubview:img1View];

// add image 2 (1024x768)
UIImage *img2 = [UIImage imageNamed:@"02.png"];
img2View = [[TapImageView alloc] initWithFrame:CGRectMake(1024, 0, 1024, 768)];
img2View.userInteractionEnabled = YES;
img2View.image = [self scaleImage:img2 by:0.24];
[container addSubview:img2View];

// scale to 24% and layout subviews
zoomed = YES;
container.transform = CGAffineTransformMakeScale(0.24, 0.24);

[self.view addSubview:container];
}

- (void)viewTapped:(id)sender
{
zoomed = !zoomed;

[UIView animateWithDuration:0.5 animations:^
{
if ( zoomed )
{
container.transform = CGAffineTransformMakeScale(0.24, 0.24);
}
else
{
img1View.image = [UIImage imageNamed:@"01.png"];
img2View.image = [UIImage imageNamed:@"02.png"];
container.transform = CGAffineTransformMakeScale(1.0, 1.0);
}
}
completion:^(BOOL finished)
{
if ( zoomed )
{
UIImage *img1 = [UIImage imageNamed:@"01.png"];
img1View.image = [self scaleImage:img1 by:0.24];
UIImage *img2 = [UIImage imageNamed:@"02.png"];
img2View.image = [self scaleImage:img2 by:0.24];
}
}];
}

这是TapImageView,它是UIImageView的子类,它通过在响应者链上发送操作来告诉我们何时点击它:
@interface TapImageView : UIImageView
@end

@implementation TapImageView

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
[[UIApplication sharedApplication] sendAction:@selector(viewTapped:) to:nil from:self forEvent:event];
}

@end

关于objective-c - 缩放包含UIView时,UIImage/UIImageView重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8837410/

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