44669

UITabBarItem image created in code doesn’t appear

Question:

I create the following View in code, the idea is to use it as an image in UITableViewCell and UITabBarItem:

- (void)drawRect:(CGRect)rect { // Fill the background with white CGContextRef context = UIGraphicsGetCurrentContext(); UIColor * whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]; CGContextSetFillColorWithColor(context, whiteColor.CGColor); CGContextFillRect(context, self.bounds); //// Color Declarations UIColor* fillColor = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0]; UIColor* strokeColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 1.0]; //// Big Oval Drawing CGRect bigOvalRect = CGRectMake(rect.origin.x+2, rect.origin.y+5, 42.0, 42.0); UIBezierPath* bigOvalPath = [UIBezierPath bezierPath]; [bigOvalPath addArcWithCenter: CGPointMake(CGRectGetMidX(bigOvalRect), CGRectGetMidY(bigOvalRect)) radius: CGRectGetWidth(bigOvalRect) / 2 startAngle: 40 * M_PI/180 endAngle: 320 * M_PI/180 clockwise: YES]; [fillColor setFill]; [bigOvalPath fill]; [strokeColor setStroke]; bigOvalPath.lineWidth = 1; [bigOvalPath stroke]; }

To create an UIImage from this UIView I am using this code:

#import <QuartzCore/QuartzCore.h> + (UIImage *) imageWithView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; }

Everything works as expected when I use it in my UITableViewCells but when I set it as the image of my UITabBarItem I only see a blue rectangle (iOS7):

MyIcon *icon = [[MyIcon alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)]; UIImage *tabImage =[self imageWithView:icon]; self.navigationController.tabBarItem.image = tabImage;

I guess it is something related with the <strong>alpha</strong> of the image but how can I fix it?

Answer1:

It wasn't a problem related to the size of the view but to its alpha.

I fixed it by setting the backgroundColor (was whiteColor) and the fillColor to:

[UIColor clearColor]

and by setting the view as opaque in the initWithFrame: method:

[self setOpaque = NO];

Recommend

  • How do I calculate radius of curvature from discrete samples?
  • Change navigation bar bottom border color Swift
  • Invert colors of image with transparent background
  • Strange behavior from affine transform rotation on iOS
  • Invert colors of image with transparent background
  • how to fill triangle in swift using Core graphics
  • how to remove all routes in google map?
  • Java Paint if x = location do this else do this
  • Sending latlng values to mysql query to update Google Maps Markers via routeboxer
  • Bing Map polyline using MVVM pattern XAML
  • Xamarin UI Test. Take control's background color ios
  • how to enable Wi-Fi and search for networks on windows phone emulator
  • How to create a customized rounded UiView and UIButton like MobilePASS?
  • Check if a program exists in bash
  • Fiware response 503 - Service unavailable
  • Automapper Enum Description Attribute
  • Shell Function string variables result blank after While loop and case?
  • Pandas - Counting quantity of commas in character field
  • BASH: Ctrl+C during input breaks current terminal
  • why does bash tell me that the command if is not found when I run the following code?
  • Git cherry pick those commits that contain a keyword (tracking id)
  • LLVM 3.0 compiler error: cast of C pointer type to Objective-C pointer type 'id' requires
  • Delete multiple lines - from “patternA” match, through second occurrence of “patternB”
  • Apple Reachability Notifications for Network OR Wi-Fi
  • C#, “Object reference not set to an instance of an object.” error
  • Drag and reorder - UICollectionview with sections
  • WebApp in AppServices vs CloudService
  • How do i find all references to a user control
  • Action Pack components in Rails
  • How to resolve this packager error on react native Android
  • Installing PHP 7 on digitalocean
  • C# fibonacci function returning errors
  • Zoom in and out of jPanel
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Problems installing Yesod for Haskell
  • Debug.DrawLine not showing in the GameView
  • Mysterious problem with floating point in LISP - time axis generation
  • Atlas images wrong size on iPad iOS 9
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • MATLAB: Piecewise function in curve fitting toolbox using fittype