1

UIViewRepresentable:

import SwiftUI
import MapKit
import CoreData

struct CustomView: UIViewRepresentable {
    
    var coordinate: CLLocationCoordinate2D
    
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        uiView.setRegion(region, animated: true)
    }
}

ContentView:

import SwiftUI
import CoreLocation

struct ContentView: View {
    var body: some View {
        GeometryReader { geo in
            CustomView(coordinate: CLLocationCoordinate2D(latitude: 37.33182, longitude: -122.03118))
                .frame(width: geo.size.width, height: 300)
                .cornerRadius(25)
                .contextMenu(/*@START_MENU_TOKEN@*/ContextMenu(menuItems: {
                    Text("Menu Item 1")
                    Text("Menu Item 2")
                    Text("Menu Item 3")
                })/*@END_MENU_TOKEN@*/)
            
        }
        .padding(.horizontal)
        .frame(height: 300)
        
    }
}

The above code works fine in the SwiftUI Canvas and the Simulator, however on my physical testing device (an iPhone 7 - iOS 14 Beta 5), when I long press the CustomView, it becomes black. The app also sometimes crashes with the following error which may be related:

CGImageCreate: invalid image alphaInfo: kCGImageAlphaNone. It should be kCGImageAlphaNoneSkipLast

If I replace the CustomView with an Image like below, everything works as expected:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Image("imageName")
        .resizable()
        .frame(width: 350, height: 300)
        .cornerRadius(25)
            .contextMenu(/*@START_MENU_TOKEN@*/ContextMenu(menuItems: {
                Text("Menu Item 1")
                Text("Menu Item 2")
                Text("Menu Item 3")
            })/*@END_MENU_TOKEN@*/)
    }
    
}

How can I fix it? Thanks!

SVP
  • 2,773
  • 3
  • 11
  • 14
  • I tried your code. It works fine in my iPhone 8 (iOS 13.6). – wzso Aug 22 '20 at 06:23
  • @BenjaminWen: it may simply be something that has to do with iOS 14 then. Thanks for taking the time to test it out! – SVP Aug 22 '20 at 06:43

0 Answers0