使用UIDropInteractionDelegate和电影

时间:2019-07-04 22:56:22

标签: swift video

我正在尝试将照片和视频拖放到我的应用中。

我使用以下代码使照片正常工作

public func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
    return session.hasItemsConforming(toTypeIdentifiers:
        [kUTTypeImage as String, kUTTypeMovie as String]) &&
        session.items.count == 1
}

public func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate
    session: UIDropSession) -> UIDropProposal {

    let dropOperation: UIDropOperation?

    if session.canLoadObjects(ofClass: UIImage.self) {
        //Make sure over drop space
    }
    else
    {
        dropOperation = .forbidden
    }

    return UIDropProposal(operation: dropOperation!)
}

public func dropInteraction(_ interaction: UIDropInteraction,
                            performDrop session: UIDropSession) {

    if session.canLoadObjects(ofClass: UIImage.self) {
        session.loadObjects(ofClass: UIImage.self) { (items) in
            if let images = items as? [UIImage] {
                //Do something with the image file
            }
        }
    }
}

我说过照片效果很好,但是不确定如何处理视频(kUTTypeMovie), “ session.canLoadObjects(ofClass:UIImage.self)”中的视频是什么类

谢谢

1 个答案:

答案 0 :(得分:0)

您可以像这样使用 itemProviders loadFileRepresentation 获取电影文件的网址

for item in session.items {
    if item.itemProvider.hasItemConformingToTypeIdentifier(kUTTypeMovie as String) {
        item.itemProvider.loadFileRepresentation(forTypeIdentifier: kUTTypeMovie as String) { (url, error) in
                
             // Copy the file to your documents directory before using it
            
        }
    }
}

记得还要将 kuTTypeMovie 添加到 canHandle 函数中

func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
    return session.hasItemsConforming(toTypeIdentifiers: [kUTTypeMovie as String])
}