gpt4 book ai didi

ios - 如何检查选定的url下是否存在jpg/pdf文件?

转载 作者:可可西里 更新时间:2023-11-01 01:08:49 27 4
gpt4 key购买 nike

如果我想检查我的 iPhone 上是否存在该文件,只需使用以下代码:

  let filePath = fileName.path
         let fileManager = FileManager.default
         if fileManager.fileExists (atPath: filePath) {

}

如何检查 URL 中是否有 pdf/jpg/png 文件:www.myname.com/files/file1.jpg 或 www.myname.com/files/file2.pdf 等?

我能问一个这样的函数的例子吗 - 但对于 Internet Web 服务器上的文件?

更新

func remoteFileExistsAt(url: URL, completion: @escaping (Bool) -> Void) {
let checkSession = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.timeoutInterval = 1.0 // Adjust to your needs

let task = checkSession.dataTask(with: request) { (data, response, error) -> Void in
if let httpResp = response as? HTTPURLResponse {
completion(httpResp.statusCode == 200)
}
}
task.resume()
}

是否可以在这个函数中检查文件是 JPG 还是 PNG 类型?如果是 - 那么我们也返回 true,如果不是,则返回 false?

最佳答案

更新:

经过评论区的讨论,更新了代码以更正确的方式工作。

您应该检查 URLResponse 对象的 mimeType 而不是检查图像是否可以表示为 UIImageJPEGRepresentation/UIImagePNGRepresentation 与否。因为它不能保证资源实际上是 jpg/jpegpng

所以mimeType应该是这里需要考虑的最可靠的参数。

enum MimeType: String {
case jpeg = "image/jpeg"
case png = "image/png"
}

func remoteResource(at url: URL, isOneOf types: [MimeType], completion: @escaping ((Bool) -> Void)) {
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let response = response as? HTTPURLResponse, response.statusCode == 200, let mimeType = response.mimeType else {
completion(false)
return
}
if types.map({ $0.rawValue }).contains(mimeType) {
completion(true)
} else {
completion(false)
}
}
task.resume()
}

用这个验证:

let jpegImageURL = URL(string: "https://vignette.wikia.nocookie.net/wingsoffire/images/5/54/Panda.jpeg/revision/latest?cb=20170205005103")!
remoteResource(at: jpegImageURL, isOneOf: [.jpeg, .png]) { (result) in
print(result) // true
}

let pngImageURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/6/69/Giant_panda_drawing.png")!
remoteResource(at: pngImageURL, isOneOf: [.jpeg, .png]) { (result) in
print(result) //true
}

let gifImageURL = URL(string: "https://media1.tenor.com/images/f88f6514b1a800bae53a8e95b7b99172/tenor.gif?itemid=4616586")!
remoteResource(at: gifImageURL, isOneOf: [.jpeg, .png]) { (result) in
print(result) //false
}

上一个答案:

您可以检查远程数据是否可以表示为 UIImageJPEGRepresentationUIImagePNGRepresentation。如果是,您可以说远程文件是 JPEGPNG

试试这个:

func remoteResource(at url: URL, isImage: @escaping ((Bool) -> Void)) {
let request = URLRequest(url: url)

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let data = data, let image = UIImage(data: data) {
if let _ = UIImageJPEGRepresentation(image, 1.0) {
isImage(true)
} else if let _ = UIImagePNGRepresentation(image) {
isImage(true)
} else {
isImage(false)
}

} else {
isImage(false)
}
}
task.resume()
}

用法:

let imageURL = URL(string: "http://domaind.com/index.php?action=GET_PHOTO&name=102537.jpg&resolution=FHD&lang=PL&region=1")!
remoteResource(at: imageURL) { (isImage) in
print(isImage) // prints true for your given link
}

关于ios - 如何检查选定的url下是否存在jpg/pdf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50480538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com