2017-01-06 2 views
0

저는 Swift의 초보자입니다.UIImage 문제에서 Tabbar 항목 이미지 추가

질문이 있습니다.

하나의 UIImage에서 Tabbar Item 이미지를 추가하면 아무 것도 나타나지 않습니다.

그러나 Tabbar Item에는 이미지가 잘 나타나고 애셋에서 탭 표시 줄 항목으로로드되는 이미지가 잘 작동합니다.

이미 이미지 크기가 조정 된 탭 막대가 아니라 크기를 조정했습니다.

그래서, 어떻게 그렇게하는지 혼란 스럽습니다.

은 여기 내 코드

이 페이스 북 프로필 사진에서로드

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이

있는 UIImage

이며,이 코드의 크기를 조정 임.

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! 
} 

이 설정 탭 표시 줄 이미지 코드

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 = "" 

} 

내가 응용 프로그램을 실행

후 바로 회색 그림 나타났다. 다른 뷰의 이미지 view.image에서이 temp_tabbar_profile을로드하면 이 잘로드되었습니다!

나는 무엇이 잘못되었는지 알지 못합니다.

내 문제를 읽어 주셔서 감사합니다.

답변

1

tabBarItems[0].image = temp_tabbar_profile.withRenderingMode(.alwaysOriginal)을 사용해야합니다. 또는 UITabBar는 추상적 인 방식으로 이미지를 표시합니다.

+0

나는 당신이 의미하는 바를 정확히 이해하지 못합니다. tabBarItem = tabBarItems [0] 이것이 맞습니까? –

+0

나는 어떻게 든 툴바를 읽는 것이 유감 스럽다. - .- 크기를 조정할 필요가없는 다른 이미지로 작동하는지 확인 했습니까? – Adarkas2302

+0

Oryour는'tabBarItems [0] .image = temp_tabbar_profile.withRenderingMode (.alwaysOriginal)'을 시도 할 수 있습니다. 나는 작은 이미지로 그것을 시도하고 잘 작동합니다. 어쩌면 크기를 조정하는 동안 이미지가 손상되었을 수 있습니다. – Adarkas2302