swiftswiftuiuivisualeffectview

Background blur effect based on image


How would you implement this background blur effect based on image?

Something like in iBooks app:

iBooks app

I was thinking about aspectFilled image in the background of VisualEffectView (since I'm supporting iOS 14) or getting 2 average colors of the image and making it gradient + blurred.


Solution

  • You can just use the same image in the background and apply a heavy blur effect to it.

    private func displayBook(imageName: String, author: String) -> some View {
        HStack {
            Image(imageName)
                .resizable()
                .scaledToFit()
            Text(author)
        }
        .padding()
        .frame(width: 150, height: 100, alignment: .leading)
        .background(
            Image(imageName)
                .resizable()
                .scaledToFill()
                .blur(radius: 30, opaque: true)
                .overlay(.background.opacity(0.4))
        )
        .clipShape(RoundedRectangle(cornerRadius: 10))
    }
    
    var body: some View {
        HStack {
            displayBook(imageName: "Goggins", author: "David Goggins")
            displayBook(imageName: "Thiel", author: "Peter Thiel")
        }
    }
    

    Screenshot