我正在尝试将照片和视频拖放到我的应用程序中。
我使用下面的代码使照片正常工作
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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,照片工作完美,但我不确定如何处理视频(kUTTypeMovie),“session.canLoadObjects(ofClass: UIImage.self)”中的视频是什么类类型
谢谢
itemProviders loadFileRepresentation您可以使用以下方法获取电影文件的 URL
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
请记住还将 kuTTypeMovie 添加到canHandle函数中
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
return session.hasItemsConforming(toTypeIdentifiers: [kUTTypeMovie as String])
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
556 次 |
| 最近记录: |