Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - SwiftUI] ImagePaint 사용 방법 (#패턴 이미지) 본문

iOS 기본 (SwiftUI)

[iOS - SwiftUI] ImagePaint 사용 방법 (#패턴 이미지)

jake-kim 2022. 10. 21. 23:05

목차) SwiftUI의 기본 - 목차 링크

ImagePaint 란?

  • ImagePaint는 이미지를 격자 무늬로 여러개 그릴때 사용하며, ShapeStyle를 준수하고 있는 형태
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@frozen public struct ImagePaint : ShapeStyle {
    public var image: Image
    public var sourceRect: CGRect
    public var scale: CGFloat

    public init(image: Image, sourceRect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1), scale: CGFloat = 1)
}
  • shapeStype을 준수하고 있으므로 fill()이나 stroke()안에 ImagePaint() 인스턴스를 넣어서 사용이 가능

ImagePaint 사용 방법

  • ShapeStyle을 준수하고 있으므로 fill, stoke에 사용
  • fill()에 사용
RoundedRectangle(cornerRadius: 20) .fill(ImagePaint(image: Image("myImage"), scale: 0.2)) 
// 0.2 - myImage크기가 크므로 작도록 스캐일 조정

  • stoke()에 사용
RoundedRectangle(cornerRadius: 20)
  .stroke(ImagePaint(image: Image("myImage"), scale: 0.2), lineWidth: 30)

  • stoke()에 sourceRect값을 주어서, stroke가 그럴듯하게 보이도록 사용
RoundedRectangle(cornerRadius: 20)
  .stroke(
    ImagePaint(
      image: Image("myImage"),
      sourceRect: CGRect(x: 0.3, y: 0.3, width: 0.5, height: 0.5),
      scale: 1.0
    ),
    lineWidth: 40
  )
  .frame(width: 300, height: 500)

* 전체 코드: https://github.com/JK0369/ExImagePaint

 

* 참고)

https://developer.apple.com/documentation/swiftui/imagepaint

https://medium.com/devtechie/imagepaint-in-swiftui-777f7fbe49ef

Comments