|
@@ -10,6 +10,8 @@ import Social
|
|
import UniformTypeIdentifiers
|
|
import UniformTypeIdentifiers
|
|
import AVFoundation
|
|
import AVFoundation
|
|
import QuickLook
|
|
import QuickLook
|
|
|
|
+import ImageIO
|
|
|
|
+import MobileCoreServices
|
|
|
|
|
|
class ShareViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UITextViewDelegate, QLPreviewControllerDataSource {
|
|
class ShareViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UITextViewDelegate, QLPreviewControllerDataSource {
|
|
let tableView = UITableView()
|
|
let tableView = UITableView()
|
|
@@ -193,19 +195,28 @@ class ShareViewController: UIViewController, UITableViewDelegate, UITableViewDat
|
|
dataShared["idContact"] = selectedContact.id
|
|
dataShared["idContact"] = selectedContact.id
|
|
dataShared["data"] = textView.text
|
|
dataShared["data"] = textView.text
|
|
if typeShareNum == TypeShare.image {
|
|
if typeShareNum == TypeShare.image {
|
|
- let compressedImageName = "Nexilis_image_\(Int(Date().timeIntervalSince1970 * 1000))_\(selectedImage != nil ? selectedImage.lastPathComponent.components(separatedBy: ".")[0] : "SS_Image").jpeg"
|
|
|
|
- let thumbName = "THUMB_Nexilis_image_\(Int(Date().timeIntervalSince1970 * 1000))_\(selectedImage != nil ? selectedImage.lastPathComponent.components(separatedBy: ".")[0] : "SS_Image").jpeg"
|
|
|
|
|
|
+ var compressedImageName = "Nexilis_image_\(Int(Date().timeIntervalSince1970 * 1000))_\(selectedImage != nil ? selectedImage.lastPathComponent.components(separatedBy: ".")[0] : "SS_Image").jpeg"
|
|
|
|
+ var thumbName = "THUMB_Nexilis_image_\(Int(Date().timeIntervalSince1970 * 1000))_\(selectedImage != nil ? selectedImage.lastPathComponent.components(separatedBy: ".")[0] : "SS_Image").jpeg"
|
|
if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShare) {
|
|
if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShare) {
|
|
- let sharedImageURL = appGroupURL.appendingPathComponent(compressedImageName)
|
|
|
|
- let sharedThumbURL = appGroupURL.appendingPathComponent(thumbName)
|
|
|
|
- if selectedImage != nil {
|
|
|
|
- try? UIImage(contentsOfFile: selectedImage.path)?.jpegData(compressionQuality: 0.25)?.write(to: sharedThumbURL)
|
|
|
|
- if let dataImage = UIImage(contentsOfFile: selectedImage.path)?.jpegData(compressionQuality: 1.0) {
|
|
|
|
- try? dataImage.write(to: sharedImageURL)
|
|
|
|
|
|
+ var sharedImageURL = appGroupURL.appendingPathComponent(compressedImageName)
|
|
|
|
+ var sharedThumbURL = appGroupURL.appendingPathComponent(thumbName)
|
|
|
|
+ if let fileURL = selectedImage {
|
|
|
|
+ thumbName = "THUMB_Nexilis_image_\(Int(Date().timeIntervalSince1970 * 1000))_\(selectedImage.lastPathComponent)"
|
|
|
|
+ sharedThumbURL = appGroupURL.appendingPathComponent(thumbName)
|
|
|
|
+ if let thumbImage = downsampleImage(at: fileURL,
|
|
|
|
+ to: CGSize(width: 300, height: 300),
|
|
|
|
+ scale: UIScreen.main.scale),
|
|
|
|
+ let thumbData = thumbImage.jpegData(compressionQuality: 0.25) {
|
|
|
|
+ try? thumbData.write(to: sharedThumbURL)
|
|
|
|
+ }
|
|
|
|
+ compressedImageName = "Nexilis_image_\(Int(Date().timeIntervalSince1970 * 1000))_\(selectedImage.lastPathComponent)"
|
|
|
|
+ sharedImageURL = appGroupURL.appendingPathComponent(compressedImageName)
|
|
|
|
+ try? FileManager.default.copyItem(at: fileURL, to: sharedImageURL)
|
|
|
|
+ } else if let uiImage = selectedImageTypeImage {
|
|
|
|
+ if let thumbData = uiImage.jpegData(compressionQuality: 0.25) {
|
|
|
|
+ try? thumbData.write(to: sharedThumbURL)
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- try? selectedImageTypeImage?.jpegData(compressionQuality: 0.25)?.write(to: sharedThumbURL)
|
|
|
|
- if let dataImage = selectedImageTypeImage?.jpegData(compressionQuality: 1.0) {
|
|
|
|
|
|
+ if let dataImage = uiImage.jpegData(compressionQuality: 1.0) {
|
|
try? dataImage.write(to: sharedImageURL)
|
|
try? dataImage.write(to: sharedImageURL)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -621,62 +632,40 @@ class ShareViewController: UIViewController, UITableViewDelegate, UITableViewDat
|
|
}
|
|
}
|
|
return
|
|
return
|
|
} else if attachment.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
|
|
} else if attachment.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
|
|
- // Handle Image
|
|
|
|
attachment.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil) { (imageItem, error) in
|
|
attachment.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil) { (imageItem, error) in
|
|
- if let imageURL = imageItem as? URL {
|
|
|
|
- DispatchQueue.main.async { [self] in
|
|
|
|
- typeShareNum = TypeShare.image
|
|
|
|
|
|
+ DispatchQueue.main.async { [self] in
|
|
|
|
+ typeShareNum = TypeShare.image
|
|
|
|
+
|
|
|
|
+ var imageToShow: UIImage?
|
|
|
|
+
|
|
|
|
+ if let imageURL = imageItem as? URL {
|
|
|
|
+ imageToShow = downsampleImage(at: imageURL,
|
|
|
|
+ to: CGSize(width: view.bounds.width, height: view.bounds.height - 150),
|
|
|
|
+ scale: UIScreen.main.scale)
|
|
selectedImage = imageURL
|
|
selectedImage = imageURL
|
|
- if let viewVc = vcHandleImage.view {
|
|
|
|
- let imageView = UIImageView()
|
|
|
|
- imageView.image = UIImage(contentsOfFile: imageURL.path)
|
|
|
|
- imageView.contentMode = .scaleAspectFit
|
|
|
|
- imageView.clipsToBounds = true
|
|
|
|
- viewVc.addSubview(imageView)
|
|
|
|
- imageView.frame = CGRect(x: 0, y: 70, width: viewVc.bounds.size.width, height: self.view.bounds.height - 150)
|
|
|
|
-
|
|
|
|
- buildAppearance(contact, viewVc)
|
|
|
|
-
|
|
|
|
- vcHandleImage.modalPresentationStyle = .fullScreen
|
|
|
|
- self.navigationController?.present(vcHandleImage, animated: true)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } else if let data = imageItem as? Data {
|
|
|
|
- DispatchQueue.main.async { [self] in
|
|
|
|
- let image = UIImage(data: data)
|
|
|
|
- typeShareNum = TypeShare.image
|
|
|
|
- selectedImageTypeImage = image
|
|
|
|
- if let viewVc = vcHandleImage.view {
|
|
|
|
- let imageView = UIImageView()
|
|
|
|
- imageView.image = image
|
|
|
|
- imageView.contentMode = .scaleAspectFit
|
|
|
|
- imageView.clipsToBounds = true
|
|
|
|
- viewVc.addSubview(imageView)
|
|
|
|
- imageView.frame = CGRect(x: 0, y: 70, width: viewVc.bounds.size.width, height: self.view.bounds.height - 150)
|
|
|
|
-
|
|
|
|
- buildAppearance(contact, viewVc)
|
|
|
|
-
|
|
|
|
- vcHandleImage.modalPresentationStyle = .fullScreen
|
|
|
|
- self.navigationController?.present(vcHandleImage, animated: true)
|
|
|
|
- }
|
|
|
|
|
|
+ } else if let data = imageItem as? Data {
|
|
|
|
+ let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString + ".tmp")
|
|
|
|
+ try? data.write(to: tempURL)
|
|
|
|
+ imageToShow = downsampleImage(at: tempURL,
|
|
|
|
+ to: CGSize(width: view.bounds.width, height: view.bounds.height - 150),
|
|
|
|
+ scale: UIScreen.main.scale)
|
|
|
|
+ selectedImageTypeImage = imageToShow
|
|
|
|
+ } else if let uiImage = imageItem as? UIImage {
|
|
|
|
+ imageToShow = uiImage
|
|
|
|
+ selectedImageTypeImage = uiImage
|
|
}
|
|
}
|
|
- } else if let image = imageItem as? UIImage {
|
|
|
|
- DispatchQueue.main.async { [self] in
|
|
|
|
- typeShareNum = TypeShare.image
|
|
|
|
- selectedImageTypeImage = image
|
|
|
|
- if let viewVc = vcHandleImage.view {
|
|
|
|
- let imageView = UIImageView()
|
|
|
|
- imageView.image = image
|
|
|
|
- imageView.contentMode = .scaleAspectFit
|
|
|
|
- imageView.clipsToBounds = true
|
|
|
|
- viewVc.addSubview(imageView)
|
|
|
|
- imageView.frame = CGRect(x: 0, y: 70, width: viewVc.bounds.size.width, height: self.view.bounds.height - 150)
|
|
|
|
-
|
|
|
|
- buildAppearance(contact, viewVc)
|
|
|
|
-
|
|
|
|
- vcHandleImage.modalPresentationStyle = .fullScreen
|
|
|
|
- self.navigationController?.present(vcHandleImage, animated: true)
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ if let safeImage = imageToShow, let viewVc = vcHandleImage.view {
|
|
|
|
+ let imageView = UIImageView(image: safeImage)
|
|
|
|
+ imageView.contentMode = .scaleAspectFit
|
|
|
|
+ imageView.clipsToBounds = true
|
|
|
|
+ imageView.frame = CGRect(x: 0, y: 70, width: viewVc.bounds.width, height: self.view.bounds.height - 150)
|
|
|
|
+ viewVc.addSubview(imageView)
|
|
|
|
+
|
|
|
|
+ buildAppearance(contact, viewVc)
|
|
|
|
+
|
|
|
|
+ vcHandleImage.modalPresentationStyle = .fullScreen
|
|
|
|
+ self.navigationController?.present(vcHandleImage, animated: true)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -823,6 +812,27 @@ class ShareViewController: UIViewController, UITableViewDelegate, UITableViewDat
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ func downsampleImage(at imageURL: URL, to pointSize: CGSize, scale: CGFloat) -> UIImage? {
|
|
|
|
+ let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
|
|
|
|
+ guard let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imageSourceOptions) else {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let maxDimensionInPixels = max(pointSize.width, pointSize.height) * scale
|
|
|
|
+ let downsampleOptions = [
|
|
|
|
+ kCGImageSourceCreateThumbnailFromImageAlways: true,
|
|
|
|
+ kCGImageSourceShouldCacheImmediately: true,
|
|
|
|
+ kCGImageSourceCreateThumbnailWithTransform: true,
|
|
|
|
+ kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels
|
|
|
|
+ ] as CFDictionary
|
|
|
|
+
|
|
|
|
+ guard let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions) else {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return UIImage(cgImage: downsampledImage)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
struct Contact {
|
|
struct Contact {
|