gpt4 book ai didi

ios - iOS 中的抗锯齿自定义栏按钮图像

转载 作者:行者123 更新时间:2023-11-29 13:09:56 26 4
gpt4 key购买 nike

我正在尝试使用自定义栏按钮图像。我创建了一个带有白色对象和透明背景的简单 PNG 图像 recommended by Apple :

enter image description here

如您所见,没有应用抗锯齿(这是我的精确图像,一个像素一个像素)。

Apple 确实推荐使用抗锯齿,但没有提供更多细节。我原以为这将是软件的一个功能,就像处理文本一样,而不是将其预先应用到图像上。

问题是——如何以编程方式为我的自定义栏按钮图像提供抗锯齿功能?

我尝试了几件事,但没有任何效果。这是我尝试过的一件事:

- (UIImage *)antialiasedImage
{
// Check to see if Antialiased Image has already been initialized
if (_antialiasedImage != nil) {
return _antialiasedImage;
}

// Get Device Scale
CGFloat scale = [[UIScreen mainScreen] scale];

// Get the Image from Resource
UIImage *buttonImage = [UIImage imageNamed:@"ReferenceFieldIcon"]; // .png???

// Get the CGImage from UIImage
CGImageRef imageRef = [buttonImage CGImage];

// Find width and height
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);



// Begin Context
UIGraphicsBeginImageContextWithOptions(buttonImage.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set Antialiasing Parameters
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldAntialias(context, YES);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

// Draw Image Ref on Context
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

// End Context
UIGraphicsEndImageContext();



// Set CGImage to UIImage
_antialiasedImage =
[UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];

// Release Image Ref
CGImageRelease(imageRef);



return _antialiasedImage;
}

然后我像这样创建分段控件:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.


NSArray *centerItemsArray =
[NSArray arrayWithObjects: @"S",
self.antialiasedImage,
@"D",
nil];


UISegmentedControl *centerSegCtrl =
[[UISegmentedControl alloc] initWithItems:centerItemsArray];

centerSegCtrl.segmentedControlStyle = UISegmentedControlStyleBar;


UIBarButtonItem *centerButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:(UIView *)centerSegCtrl];


UIBarButtonItem *flexibleSpace =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];




NSArray *barArray = [NSArray arrayWithObjects: flexibleSpace,
centerButtonItem,
flexibleSpace,
nil];

[self setToolbarItems:barArray];
}

提前致谢!

最佳答案

IOS 不会为呈现的图像添加抗锯齿功能——只有从代码中提取的项目。在保存到文件之前,您必须消除图像的锯齿。

从代码中消除图像锯齿的方法是在代码中绘制它们。

关于ios - iOS 中的抗锯齿自定义栏按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17635480/

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