2

In my app, I have a method that transforms a UIView into a UIImage. It works fine on every device, except on the iPhone 6 plus, which has a higher pixel density.

I am creating the selectionIndicatorImage for my UITabBar by transforming a grey view into a UIImage. I know this is probably not the best practice in this scenario but anyway I need this method working fine for other things on the application.

This is my UIImage initializer:

convenience init?(view: UIView) {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())

    var img = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    self.init(CGImage: img.CGImage)
}

This generates the following results:

iPhone 6 Plus

iPhone 6 Plus

iPhone 6

iPhone 6

Any thoughts?

Cheers

Marcos Duarte
  • 3,472
  • 4
  • 19
  • 22

2 Answers2

0

//Look For Line.Hope this helps you

"The simplest way is to set the frame of your UIImageView and set the contentMode to one of the " The simplest way to resize an UIImage?

Community
  • 1
  • 1
Mukesh
  • 3,680
  • 1
  • 15
  • 32
  • Should I need to resize it? I though that setting the scale to 0.0 on `UIGraphicsBeginImageContextWithOptions` would make it resize to the proper size for the screen – Marcos Duarte May 19 '15 at 00:53
0

Assuming that you are calculating tabBarFrame in viewDidLoad method, when tab bar is not created yet, the frame property will give you wrong results. You need to move your code in viewDidAppear method or if you want to do your calcuation earlier you can also move it in viewWillAppear but call tabBar.layoutIfNeeded() first

Zell B.
  • 10,266
  • 3
  • 40
  • 49