URL前缀的不同操作 [英] Different action by prefix of URL
问题描述
以下是我的情况:我正在我的ios应用程序本地调用文件(在Swift中运行)。
Here is my situation: i'm calling file locally on my ios application ( Running in Swift).
如果文件是jpg,则会发生一个动作,如果文件是mp4,则会发生另一个动作。
If the file is a jpg, one action happen, if the file is a mp4, another action happen.
对于这个我正在使用此代码:
For this i'musing this code:
let urlString = "\(posts[selectedIndexPath].link)"
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileName = urlString as NSString;
let filePath="\(documentsPath)/\(fileName.lastPathComponent)";
let fileURL = NSURL.init(fileURLWithPath: filePath)
let request = NSURLRequest.init(url: fileURL as URL)
/* END DOWNLOAD + READ LOCALY */
if (fileURL.pathExtension?.hasPrefix("jpg"))! {
Swift.print("THIS IS A JPG")
}
else if (fileURL.pathExtension == "mp4") {
Swift.print("THIS IS A MP4")
}
这非常有效。
我现在需要做的不是在本地调用eifle,而是通过URL调用它。
What i need to do now is instead of calling th eifle locally, to calling it form an URL.
我通过以下网址从网址读取我的文件:
I read my file from an url by:
videoVRView.load(from: URL(string: "\(posts[selectedIndexPath].link)")
哪项工作。
但是,由此,行动无效,我尝试以下方法:
But from that, the action is not working, i've try the following:
if ((from: URL(string: "\(posts[selectedIndexPath].link)").hasPrefix("jpg"))! {
Swift.print("THIS IS A JPG")
}
else if ((from: URL(string: "\(posts[selectedIndexPath].link)") == "mp4") {
Swift.print("THIS IS A MP4")
}
没有任何成功!!
有人知道这是如何实现的吗?
Does anybody know how is this achievable ?
非常感谢=)
- 编辑 -
我想要做的是继续恢复:
What im trying to do is th efollowing to resume:
at th emoment我通过以下方式在本地调用图像:
at th emoment i call image locally via:
imageVRView.load(UIImage(named: "\(documentsPath)/\(fileName.lastPathComponent)" ),
of: GVRPanoramaImageType.stereoOverUnder)
我尝试使用:
imageVRView.load(UIImage(named: "\(posts[selectedIndexPath].link)" ),
of: GVRPanoramaImageType.stereoOverUnder)
没有成功。 。 。 。我需要通过这种方法调用图像...任何想法?
非常感谢!
Without success . . . . I need to call the image via this method ... any idea ? Thanks a lot !
推荐答案
您可以使用httpMethod HEAD来检查您的网址请求url mime类型,无需先下载数据:
You can make a URL request for the url header using the httpMethod HEAD to check your url mime type without the need to download the data first:
let link = "https://www.dropbox.com/s/sk46eyglvijlrec/horse.jpg?dl=1"
let url = URL(string: link)!
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
URLSession.shared.dataTask(with: request) { _ , response , _ in
guard let response = response, (response as? HTTPURLResponse)?.statusCode == 200 else { return }
DispatchQueue.main.async() {
print("mimeType", response.mimeType ?? "nil") // image/jpeg
print("suggestedFilename:", response.suggestedFilename ?? "no suggestedFilename") // horse.jpg
print("expectedContentLength:", response.expectedContentLength ?? "nil") // 352614
print("textEncodingName:", response.textEncodingName ?? "nil")
print("url:", response.url ?? "nil") // "https://dl.dropboxusercontent.com/content_link/RNrhGtvroTLU1Gww7eQo1N1ePRiix68zsqZJ1xWPjKm3pmOUNQwNVntbPuFG4jZ8/file?dl=1"
}
}.resume()
这篇关于URL前缀的不同操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!