TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Day 78 Challenge: Image(from uiimage) and map view make map panning lag

Forums > 100 Days of SwiftUI

@Bnerd  

In Day 78 challenge ( SaveContact App) I have added a detail view that when an item on the list is tapped it will navigate in a view that shows the Image on the top and the annotated Map at the bottom. The view works, but, when I try to pan on the map the map is sluggish.. For the image I am using a computable Image variable, that reads the .jpg file from the disk. Debugging, I noticed that if I just place an image and place it in Assets.xcassets and try to load it in the detail view with the map, the map works flawlessy! Did someone notice something similar ? Below is my detail view code in case you notice something too obvious..

import SwiftUI
import MapKit

struct ItemDetailView: View {

    var itemName: String
    var itemImage: Image {
        let uiImage = (UIImage(data: try! Data(contentsOf: FileManager.documentsDirectory.appendingPathComponent("\(itemName).jpg")))!)
        return Image(uiImage: uiImage)
    }

    @ObservedObject var viewModel = ContentView.ViewModel() //My viewModel is an extension of the ContentView
    var imageLocationLatidude: Double
    var imageLocationLongitude: Double

    var body: some View {
        VStack {
            itemImage.resizable().scaledToFit().clipShape(RoundedRectangle(cornerRadius: 20)).padding()
//            Image("sample").resizable().scaledToFit().clipShape(RoundedRectangle(cornerRadius: 20)).padding() //This works perfectly with the map.
            //***Map Start
            Map(coordinateRegion: $viewModel.mapRegion, annotationItems: viewModel.contacts) { _ in
                MapMarker(coordinate: CLLocationCoordinate2D(latitude: imageLocationLatidude, longitude: imageLocationLongitude))
            }.clipShape(RoundedRectangle(cornerRadius: 20)).padding(.horizontal)
            //Map End***
        }
        .onAppear { // I use the code below to re-center the map at location of the annotation.
            viewModel.mapRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: imageLocationLatidude, longitude: imageLocationLongitude), span: MKCoordinateSpan(latitudeDelta: 25, longitudeDelta: 25))
        }
    }
}

2      

I'm kind of guessing here, but it is probably because your image is a computed property.

So, every time you move the map a slight amount, the View would have to reinvoke its body, and compute the property (render the image from data) again.

That seems like it would slow things down quite a bit.

2      

@Ostrich observes:

So, every time you move the map a slight amount,
the View would have to reload itself and compute the property
(render the image from data) again.

Oh nice! I've always wanted to find a reason to learn how to use the Instruments application provided with Xcode. One of the instruments is a disk access monitor. This might be a great use case to test!

If it works the way I think it should, you launch Instruments and watch your application whilst it's running in the simulator. Perhaps as you pan around the map, you might, as @Ostrich predicts, you might see a ton of data being loaded and reloaded. This might be a great way to test this assumption!

2      

@Bnerd  

I guess the reloading of the Image is the culprit.. How do I stop it from reloading all the time? Maybe if I make a separate View for the map and add that one in the Vstack?

2      

You could probably do it by just declaring itemImage as an Image without initializing it. So, it would no longer be a computed property. Then you would need to write a custom initializer for ItemDetailView that takes care of initializing itemImage.

2      

@Bnerd  

Thanks for the help guys. The computable variable was the reason for the poor performance..I just moved the computation of the UIImage into the NavigationLink (in my contentView) ItemDetailView Struct and issue solved, no more constant reloading of the image..

4      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.