I am trying to get the URL for the image associated with a given URL. Using Apple's LPLinkMetadata
, I am able to get the URL's title
and description
, but I cannot figure out how to access the metadata's image URL. I have access to data.imageProvider
but I am not sure how to use it.
import LinkPresentation
final class URLHelper {
@available(iOS 13.0, *)
static func fetchURLPreview(url: URL) {
let metadataProvider = LPMetadataProvider()
metadataProvider.startFetchingMetadata(for: url) { (metadata, error) in
DispatchQueue.main.async {
if let _ = error {
// handle error
} else if let data = metadata {
let urlTitle = data.title
let urlImageUrl = data.????????
}
}
}
}
}
Check below my code for get image URL from LPLinkMetadata.
let metadataProvider = LPMetadataProvider()
let url = URL(string: "https://www.facebook.com/")!
metadataProvider.startFetchingMetadata(for: url) { metadata, error in
if error != nil {
return
}
metadata?.imageProvider?.loadFileRepresentation(forTypeIdentifier: kUTTypeImage as String, completionHandler: { (url, imageProviderError) in
if imageProviderError != nil {
return
}
let myImage = UIImage(contentsOfFile: (url?.path)!)
})
}
let _ = md.imageProvider?.loadObject(ofClass: UIImage.self, completionHandler: { image, err in
DispatchQueue.main.async {
self?.imageViewWebSite.image = image as? UIImage
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.