Note that there are some explanatory texts on larger screens.

plurals
  1. POAnti-aliasing custom bar button image in iOS
    primarykey
    data
    text
    <p>I'm trying to use a custom bar button image. I've created a simple PNG image with a white object and a transparent background as <a href="http://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html#//apple_ref/doc/uid/TP40006556-CH14-SW8" rel="nofollow noreferrer">recommended by Apple</a>:</p> <p><img src="https://i.stack.imgur.com/7yoph.png" alt="enter image description here"></p> <p>As you can see, there is no anti-aliasing being applied (that is my exact image, pixel for pixel).</p> <p>Apple does recommend to use Anti-aliasing, but don't provide any more detail. I would have thought this would be a function of the software like it would with text, rather than pre-applying it to the image.</p> <p>The question is--how can I programatically provide anti-aliasing for my custom bar button images?</p> <p>I have tried several things but nothing is doing it for me. Here's one thing I tried:</p> <pre><code>- (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; } </code></pre> <p>Then I create my Segmented Control Like So:</p> <pre><code>- (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]; } </code></pre> <p>Thank you in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload