SwiftUI: UIActivityViewController share JPEG data from UIImage

Hello everybody,

I am currently implementing the sharing capability of my application. I want to share an image in JPEG format with text.

The problem is that when I use image.jpegData(compressionQuality: 0.8) and I try to post it on Twitter the popup does not show up. This does not happens when converting to pngData. I find this odd and I don’t know if this has happened to any of you but I an error about dismissing the view.

Here is my UIActivityViewController:

Code Block
import SwiftUI
struct ShareSheet: UIViewControllerRepresentable {
var items: [Any]
func makeUIViewController(context: UIViewControllerRepresentableContext<ShareSheet>) -> UIActivityViewController {
let controller = UIActivityViewController(activityItems: items, applicationActivities: nil)
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ShareSheet>) {}
}


I am creating my items array like this:

Code Block
func getItemShareSheet() -> [Any] {
let species = observation.speciesName
let image = observation.image.pngData()
let date = observation.date
var finalText: String = ""
if let location = observation.location {
let latitude = String(format: "%.1f", Double(location.coordinate.latitude))
let longitude = String(format: "%.1f", Double(location.coordinate.longitude))
finalText = "New observation - \(date)\nSpecies: \(species)\nLatitude: \(latitude)\nLongitude: \(longitude)"
}
else {
finalText = "New observation - \(date)\nSpecies: \(species)\n"
}
let items: [Any] = [image!, finalText]
return items
}


Has this happen to any of you?

Thank you!
Answered by tmsm1999 in 638488022
Hello everybody,

This is the solution I found:

The best way is to resize the image bellow 90%. Here is the code I used. I found it on StackOverflow.

Code Block
extension UIImage {
func resized(withPercentage percentage: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: size.width * percentage, height: size.height * percentage)
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
func resized(toWidth width: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
}


Accepted Answer
Hello everybody,

This is the solution I found:

The best way is to resize the image bellow 90%. Here is the code I used. I found it on StackOverflow.

Code Block
extension UIImage {
func resized(withPercentage percentage: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: size.width * percentage, height: size.height * percentage)
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
func resized(toWidth width: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
}


SwiftUI: UIActivityViewController share JPEG data from UIImage
 
 
Q