2014-10-04 5 views

답변

9

이렇게하려면 먼저 이미지를 viewcontroller에서 사용할 수있게하십시오 (스토리 보드의 이미지와 뷰 컨트롤러 사이의 연결을 만들어야합니다).

imageView라고 가정합니다.

프로그래밍, 당신은 말할 수 :

imageView.image = UIImage(named: ("nameofasset")) 

을 실제로 같은 반환 할 이미지를 결정하기위한 책임 정적 기능을 만드는 일을 결국 무엇을 : 그래서 지금

class func GetImage(someValueWhichDrivesLogic: Int)->UIImage 
{ 
    if (someValueWhichDrivesLogic == 1) 
    { 
     assetName = "imageone" //note you don't add .png, you just give it the same name you have in your standard images folder in XCode6 
    } 
    else if (someValueWhichDrivesLogic == 2) 
    { 
     assetName = "imagetwo" //again, this is the actual name of my image 
    } 

    //Return an image constructed from what existed in my images folder based on logic above 
    return UIImage(named: (assetName)) 

} 

을하는 기능 위와 같은 내용이 작성되면 다음과 같이 할 수 있습니다.

imageView.image = GetImage(1) 
imageView.image = GetImage(2) 

T 실제로 이미지에 이미지를 동적으로 할당합니다. 당신은 단지 이것을 할 수 있습니다, 또는 만약 당신이 문자 그대로 버튼에 넣는 것만 큼 간단하게하는 것이라면, 위에서 지정한 더 쉬운 접근법을 취할 수 있습니다.

질문이 있으면 알려주세요.