Share Extension don't open/share .HEIC type image

572 views Asked by At

i created share extension for my app which can share image/videos from photos(Gallery) and it work properly but here problem is if i select .HEIC type file then click on my share extension it show error as shown in below screenshot .

enter image description here

import UIKit
import Social
import MobileCoreServices
import AVKit
import Toast_Swift

//@objc(ShareExtensionViewController)
class ShareViewController: UIViewController {
    @IBOutlet weak var image: UIImageView!
    @IBOutlet weak var btn: UIButton!
    @IBOutlet weak var lbl: UILabel!
    @IBOutlet weak var view2: UIView!
    @IBOutlet weak var cancel: UIButton!
    @IBOutlet weak var blurview: UIVisualEffectView!
    
    var allMedia = [Data()]
    var singleImage = UIImage()
    var imagesUrl = [URL]()
    let groupPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "
my group identifire ")
    var saveDone = Bool()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let attachments = (self.extensionContext?.inputItems.first as? NSExtensionItem)?.attachments ?? []

        self.btn.setTitle("Add", for: .normal)
        self.btn.layer.cornerRadius = 10
        self.cancel.layer.cornerRadius = 10
        self.image.clipsToBounds = false
        applyshadow(image: image)
        blurview.isHidden = true
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        self.handleSharedFile()
    }
    
//    MARK:- function
    
    func applyshadow(image: UIImageView) {
        image.clipsToBounds = false
        image.layer.shadowColor = UIColor.systemGray.cgColor
        image.layer.shadowOpacity = 1
        image.layer.shadowOffset = CGSize.zero
        image.layer.shadowRadius = 7.5
    }
    
    func videotext(string : String) ->Bool{
        let videoextension = [".MP4",".mp4",".mkv",".MKV",".AVI",".avi",".mov", ".MOV"]
        if videoextension.contains(string){
            return true
        }
        return false
    }
    
    func videoThumb(filepath: URL) -> UIImage{
        do{
//            let filepath = data?.appendingPathComponent(lbl[indexPath.row])
            let asset = AVURLAsset(url: filepath, options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
            let uiImage = UIImage(cgImage: cgImage)
            return uiImage
        }catch let error {
            print("Error: \(error.localizedDescription)")
            return UIImage()
        }
    }
    
    private func handleSharedFile() {
        // extracting the path to the URL that is being shared
        let attachments = (self.extensionContext?.inputItems.first as? NSExtensionItem)?.attachments ?? []
        let contentType = kUTTypeData as String
        for provider in attachments {
            // Check if the content type is the same as we expected
            if provider.hasItemConformingToTypeIdentifier(contentType) {
                provider.loadItem(forTypeIdentifier: contentType,
                                  options: nil) { [unowned self] (data, error) in
                    guard error == nil else { return }
                    if let url = data as? URL {
                        imagesUrl.append(url)
                        print(imagesUrl)
                        if videotext(string: String(url.lastPathComponent.suffix(4))){
                            DispatchQueue.main.async {
                                self.image.image = videoThumb(filepath: url)
                                if attachments.count > 1 {
                                    lbl.text = "\(attachments.count) Items"
                                }else{
                                    lbl.text = url.lastPathComponent
                                }
                            }
                        }else {
                            let imageData = try? Data(contentsOf: url)
                            DispatchQueue.main.async {
                                if provider == attachments.last{
                                    self.image.image = UIImage(data: imageData!)
                                }
                                self.singleImage = UIImage(data: imageData!)!
                                if attachments.count > 1 {
                                    lbl.text = "\(attachments.count) Items"
                                }else{
                                    lbl.text = url.lastPathComponent
                                }
                            }
                        }
                    }else {
                        print("Impossible to save image")
                    }
                }
            }
        }
    }

thank You in advance for read, giving attention, answer and upvote my question :)

0

There are 0 answers