gpt4 book ai didi

ios - 如何在 iOS 中放大 UIImage 以清除像素而不变得模糊?

转载 作者:行者123 更新时间:2023-12-02 02:28:55 25 4
gpt4 key购买 nike

我需要制作一个UIImageView支持放大/缩小,最重要的是,当图像放大时,应该看到清晰的大像素,而不是模糊处理的效果,如下图所示。

应该是这样的:

enter image description here

不应该是这样的:

enter image description here

我的代码不起作用:

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

imageView = [[ColorPickImageView alloc] initWithImage:self.image];
imageView.contentMode = UIViewContentModeScaleAspectFit;

[self addGestureRecognizerToView:imageView];
[imageView setUserInteractionEnabled:YES];
[imageView setMultipleTouchEnabled:YES];

[self.view addSubview:imageView];

[imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(imageView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[imageView]-0-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]-0-|" options:0 metrics:nil views:views]];
}

- (void)addGestureRecognizerToView:(UIView *)view
{
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[view addGestureRecognizer:pinchGestureRecognizer];
}

- (void)pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
UIView *view = pinchGestureRecognizer.view;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1;
}
}

最佳答案

好吧,我很感兴趣,忍不住设置了一个 15 分钟的快速测试。结果符合the SO answer我已经在评论中引用了。以下内容适合我生成像素化图像:

- (void)viewDidLoad {
[super viewDidLoad];

UIImage *image = [UIImage imageNamed:@"LocationDot"];
self.imageView.image = image;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.layer.magnificationFilter = kCAFilterNearest;
// self.imageView.layer.shouldRasterize = YES;
// self.imageView.transform = CGAffineTransformMakeScale( 4, 4);
}

这会生成:

pixelated image

使用默认的kCAFilterLinear,你会得到:

enter image description here

应用变换时,也达到了预期的效果:

enter image description here

正如预期的那样,shouldRasterize 没有任何效果。使用位图作为内容,只有在图层上进行变换然后与另一个图层进行合成时才会产生一些效果。即便如此,documentation说:

If the rasterized bitmap requires scaling during compositing, the filters in the minificationFilter and magnificationFilter properties are applied as needed.

关于ios - 如何在 iOS 中放大 UIImage 以清除像素而不变得模糊?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203371/

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