[SwiftUI] Image()

Written by 최승기 on May 1st, 2021 Views Report Post

Table of contents

Image()

이미지는 xcode에 미리 등록해야한다.

  • Assets 파일에 등록한다.
  • 예) SampleImage

참고) 뭔가 에러가 생기면 Clean build folder를 이용한다.

Command+Shift+K

Code

Image("SampleImage") // 어쎗파일에 등록된 이름을 사용한다.
 .resizable()
 .aspectRatio(contentMode: .fill)
 .frame(width:300,height:200)
 .clipped()
  // 위의 4가지 조합이 일반적인 조합이다. 
  // 만일 .fit 을 사용하는 경우 .clipped()는 필요 없다.
 .scaledToFill(), .scaledToFit()
  // aspectRatio 대신 사용할 수 있다.  

 .cornerRadius(30) //clipped() 대신 사용함 
 
 .frame(width:300, height:300) 
 .cornerRadius(150) // frame 크기의 절반 
  // 이렇게 하면 이미지를 원으로 표현할 수 있다. 

 // cornerRadius를 사용하지 않고 clipShape을 사용할 수 있다. 
 // 어떤 모양으로 해도 된다.  
 .clipShape( 
   Circle()
   RoundedRectangle(cornerRadius: 25.0)
   Ellipse() 
  )

 .foregroundColor(.red) // 이것으로 색이 바꿀려면 
 .renderingMode(.template) // 을 resizable()전에 둔다.
  // 이미지가 투명한 배경이 있는 경우 작용한다. 
  // 이것을 입력하고 싶지 않으면 
  // 어쎗에서 Render As 를 template Image 로 설정한다. 

스크린샷 2021-05-01 오후 8.55.40.png

Comments (0)