swiftui - ImageRenderer omits curved text - Bug? - Stack Overflow

I am using the CrookedText library to generate curved text around a circle.My app is supposed to print

I am using the CrookedText library to generate curved text around a circle.

My app is supposed to print the image that is generated via ImageRenderer, but fails to include the curved text, although it is properly rendered on the screen when I present the same view:

Here's an image with the current result (top image) and the expected output (bottom image):

The code:

struct CardPresenterView: View {
    
    @State private var renderedImage = Image(systemName: "photo")
    
    var body: some View {
        VStack() {
            Spacer()
            OptionView()
            Spacer()
            renderedImage
                .shadow(radius: 10)
            Spacer()
            CardView()
               .aspectRatio(2/3, contentMode: .fit)
            
           Spacer()
               ShareLink("Export", item: renderedImage, preview: SharePreview(Text("Shared image"), image: renderedImage))
        }
        .onAppear { render() }
    }
    
    @MainActor func render() {
         let renderer = ImageRenderer(content: CardView().aspectRatio(2/3, contentMode: .fit))
         if let uiImage = renderer.uiImage {
             renderedImage = Image(uiImage: uiImage)
         }
     }
}

I cannot explain this behavior, is this an XCode bug?

I am using the CrookedText library to generate curved text around a circle.

My app is supposed to print the image that is generated via ImageRenderer, but fails to include the curved text, although it is properly rendered on the screen when I present the same view:

Here's an image with the current result (top image) and the expected output (bottom image):

The code:

struct CardPresenterView: View {
    
    @State private var renderedImage = Image(systemName: "photo")
    
    var body: some View {
        VStack() {
            Spacer()
            OptionView()
            Spacer()
            renderedImage
                .shadow(radius: 10)
            Spacer()
            CardView()
               .aspectRatio(2/3, contentMode: .fit)
            
           Spacer()
               ShareLink("Export", item: renderedImage, preview: SharePreview(Text("Shared image"), image: renderedImage))
        }
        .onAppear { render() }
    }
    
    @MainActor func render() {
         let renderer = ImageRenderer(content: CardView().aspectRatio(2/3, contentMode: .fit))
         if let uiImage = renderer.uiImage {
             renderedImage = Image(uiImage: uiImage)
         }
     }
}

I cannot explain this behavior, is this an XCode bug?

Share Improve this question edited Jan 29 at 6:07 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Jan 29 at 6:02 Nathanael TseNathanael Tse 3631 silver badge14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If the library would be using a Canvas to draw the curved text then, according to the ImageRenderer documentation, it should work. So it must be drawing it in some way which is not compatible with ImageRenderer.

You could try using CurvedText from this answer instead. This is a SwiftUI solution for curved text (it was my answer). Doing it this way, the rendered version seems to work fine, as the following (improvised) version of CardView demonstrates. The values for .trim and .rotationEffect were found with a little trial and error:

struct CardView: View {
    var body: some View {
        ZStack {
            Circle()
                .fill(.yellow)
                .frame(width: 300, height: 300)
            Circle()
                .trim(from: 0, to: 0.28)
                .stroke(.red, lineWidth: 40)
                .rotationEffect(.degrees(-0.58 * 360))
                .padding(20)
            Circle()
                .trim(from: 0, to: 0.32)
                .stroke(.red, lineWidth: 40)
                .rotationEffect(.degrees(-0.2 * 360))
                .padding(20)

            // See https://stackoverflow/a/77280669/20386264
            CurvedText(string: "The quick brown fox", radius: 130)
                .font(.title2)
                .offset(y: -130)
                .rotationEffect(.degrees(-0.19 * 360))
            CurvedText(string: "jumps over the lazy dog", radius: 130)
                .font(.title2)
                .offset(y: -130)
                .rotationEffect(.degrees(0.21 * 360))
        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309625a4621924.html

相关推荐

  • swiftui - ImageRenderer omits curved text - Bug? - Stack Overflow

    I am using the CrookedText library to generate curved text around a circle.My app is supposed to print

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信