我试图实现拖放照片和视频到我的应用程序。
我已经使照片工作使用下面的代码
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:用户界面图像.self)”
谢谢
3条答案
按热度按时间f3temu5u1#
您可以使用
itemProviders
loadFileRepresentation
获取电影文件的URL,如下所示记住还要将kuTTypeMovie添加到
canHandle
函数中svmlkihl2#
2022 ...类型标识符的语法已更改:
您需要:
llycmphe3#
为了添加到@torof的解决方案中,我完成了他在代码注解中提到的复制部分。作为文件目标,我使用
.cachesDirectory
,因为我不想永久存储文件,而只是为了在此会话中使用,就在回调之外。这样,当需要空间时,操作系统会自动清理它。