2

I have an app that saves created user events with a title, date and an image for personal use. This is stored with Core Data so the Image is stored as Data. I have a shared App Group that I am trying to write the file to and then display it within the Live Activity. The writing of the file seems to go through, but the Live Activity never starts and I receive an error in creating the Live Activity.

When I keep the IF statement in the View, but change the image within the statement to an asset, it actually presents the image, so it seems like the function for getting Data to UIImage also works. But when I set the let image as the image inside the if statement it doesn't create the Live Activity.

Thank you so much for any contributions here. (Apologies for Beginner-knowledge here. Starting out :) )

This is the creation of the Live Activity and functions: `

class EventModel: ObservableObject {
    @Published var event: EventEntity
    var liveActivity: Activity<EventAttributes>? = nil

    func startLiveActivity() {
        
        let _ = saveImage(data: (event?.image)!)
        
        let liveEvent = LiveActivityEvent(title: (event?.title)!, date: (event?.date)!)
        let attributes = EventAttributes(event: liveEvent)
        
        do {
            liveActivity = try Activity.request(attributes: attributes, contentState: EventAttributes.ContentState())
        } catch {
            print(error.localizedDescription)
        }
    }
}

func saveImage(data: Data) -> Bool {
    
    guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "AppGroupIdentifier") else {
        fatalError("could not get shared app group directory.")
    }
    
    do {
        try data.write(to: groupURL.appendingPathComponent("LiveActivityPhoto"))
        return true
    } catch {
        print(error.localizedDescription)
        return false
    }
    
}

func getSavedImage(named: String) -> UIImage? {
    if let dir = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "AppGroupIdentifier") {
        return UIImage(contentsOfFile: URL(fileURLWithPath: dir.absoluteString).appendingPathComponent(named).path)
    }
    return nil
}

`

And this is the Live Activity View `

struct LiveActivityView: View {
    
    var context: ActivityViewContext<EventAttributes>
    
    var body: some View {
        HStack {
            
            if let image = getSavedImage(named: "LiveActivityPhoto") {
                Image(uiImage: image)
             
            }
        }
    }
}


`

2
  • Could you add a Text in LiveActivityView with only the path your searching for the image and verify that the image is actually there? Commented Jan 18, 2023 at 12:04
  • hi mate, have you found a solution for this? Struggling with the same issue; it looks like we can use pre-defined images only, right? Commented Oct 31, 2023 at 18:45

1 Answer 1

0

I have found a solution based on the App Group

  1. Add App Group capability for both targets, main and widget (Project -> Target -> Signing&Capability tab -> find “+” sign. The app group should have the same name

  2. Then you can save any image to the destination URL like this

    private func uploadImage() {
    guard var destination = FileManager.default.containerURL(
      forSecurityApplicationGroupIdentifier: "group.YOUR_GROUP_NAME")
    else { return }
    
    destination = destination.appendingPathComponent("imageName")
    
    let image = UIImage(named: "kopiumTest")!
    if let imageData = image.jpegData(compressionQuality: 0.5) {
      try? imageData.write(to: destination)
    }}
    

and then you will be able to use just saved image in the Widget

@ViewBuilder var uploadIcon: some View {
  let imageContainer = FileManager.default.containerURL(
    forSecurityApplicationGroupIdentifier: "group.YOUR_GROUP_NAME")?
    .appendingPathComponent("imageName"),
  let uiImage = UIImage(contentsOfFile: imageContainer.path()) {
    Image(uiImage: uiImage)
      .resizable()
      .aspectRatio(contentMode: .fill)
      .frame(width: 50, height: 50)
  } else {
    Text("Not found")
  }
}

Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, does this still work? I keep getting a gray box instead of the image showing up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.