I'm Newbie in Swift.
I have a question.
When I add Tabbar Item image from one UIImage, then Nothing show up.
Although, Not in Tabbar Item, Image appear well and image that load from asset to tab bar item works well.
I already resize that thing, and not in tab bar, that resized image load well.
So, I'm so confused how to do that.
Here is my codes
This is load from Facebook profile picture
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "email, id, name, picture"]).start { (connection, result, err) in
if err != nil {
print("FBLogin Error: \(err)")
return
}
print(result!)
let dd = result as! [String : AnyObject]
let test = (dd["picture"]!["data"]!! as! [String: AnyObject])["url"]
let url = URL(string: test as! String)
let data_picture = try? Data(contentsOf: url!)
var temp_img = UIImage()
temp_img = UIImage(data: data_picture!)!
temp_tabbar_profile = self.resizeImage(image: temp_img, targetSize: CGSize(width: 30, height: 30))
temp_tabbar_profile is UIImage
And This is resize code.
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
This is set tab bar image code
override func viewDidLoad() {
super.viewDidLoad()
let tabBarItems = tabBar.items! as [UITabBarItem]
tabBarItems[0].selectedImage = temp_tabbar_profile
tabBarItems[0].image = temp_tabbar_profile
tabBarItems[0].title = ""
}
When I run app, then just gray picture appeared. When I load this temp_tabbar_profile in other view's image view.image, loaded well!!!
I don't know what is wrong.
Thank you for reading my problem.
You should use
tabBarItems[0].image = temp_tabbar_profile.withRenderingMode(.alwaysOriginal)
. Or the UITabBar will display your image in a abstract way.