gpt4 book ai didi

ios - Swift mapview 无法显示缩略图

转载 作者:行者123 更新时间:2023-12-01 16:05:50 25 4
gpt4 key购买 nike

我正在将图像写入文件并在 mapview didSelect 方法中读回,它给出了图像数据读取错误。

 var localImageFileUrl: URL? = nil

do {
delegate.documentDirectoryUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

if let data = aStruct.imageData {
localImageFileUrl = delegate.documentDirectoryUrl!.appendingPathComponent("Image.png")
UserDefaults.standard.set(localImageFileUrl, forKey: "localImageFileUrl")
try data.write(to: localImageFileUrl!)
}

}
catch {
print("error")
}

在这里测试它工作正常。
          do{
print("imageUrl0: \(localImageFileUrl)")
let imageData: Data = try Data(contentsOf: localImageFileUrl as! URL)
let image = UIImage(data: imageData)
print(image)
}catch{
print("Unable to load image data: \(error)")
}

调试窗口输出:

imageUrl0: Optional(file:///var/mobile/Containers/Data/Application/19A5A226-3CAF-4DC0-9015-40278703C5BA/Documents/Image.png)



可选(, {120, 120})
工作正常。

我通过这样的结构传递数据:
   let endPtPin = GpsData.MyEndPt(latitude:  Double(latitudeStr) as! Double, longitude: Double(longitudeStr) as! Double, url: localImageFileUrl! as NSURL)

在 mapview didSelect 方法中,我提取了 url,并尝试访问图像,但它给出了错误:
 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

DispatchQueue.main.async {
if let thumbnailImageButton = view.leftCalloutAccessoryView as? UIButton,
let url = (view.annotation as? GpsData.MyEndPt)?.thumbnailUrl
{
print("imageUrl: \(url)")
do{
let imageData: Data = try Data(contentsOf: url as URL)
let image = UIImage(data: imageData)
thumbnailImageButton.setImage(image, for: UIControlState.normal)

}catch{
print("Unable to load image data: \(error)")
}
}
}
}

调试窗口给出错误:

imageUrl: Optional(file:///var/mobile/Containers/Data/Application/19A5A226-3CAF-4DC0-9015-40278703C5BA/Documents/Image.png) 2020-03-27 00:07:04.079576-0500 r2nr[9945:4518812] NSURLConnection finished with error - code -1002

2020-03-27 00:07:07.009134-0500 r2nr[9945:4518751] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme

Unable to load image data: Error Domain=NSCocoaErrorDomain Code=256 "The file “Image.png)” couldn’t be opened." UserInfo={NSURL=Optional(file:///var/mobile/Containers/Data/Application/19A5A226-3CAF-4DC0-9015-40278703C5BA/Documents/Image.png)}



文件路径没有改变,但无法从文件中读取图像数据。怎么了?

最佳答案

我花了几个小时,找到了工作的方法。
我正在访问图像数据作为数据

if let data = aStruct.imageData  {
localImageFileUrl = delegate.documentDirectoryUrl!.appendingPathComponent("Image.png")
UserDefaults.standard.set(localImageFileUrl, forKey: "localImageFileUrl")
try data.write(to: localImageFileUrl!)
}

我像这样将数据转换为 PNG 格式,并且效果很好:
  do {
delegate.documentDirectoryUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let image = UIImage(data: aContactStruct.imageData!)

if let data = UIImagePNGRepresentation(image!){{
localImageFileUrl = delegate.documentDirectoryUrl!.appendingPathComponent("Image.png")
UserDefaults.standard.set(localImageFileUrl, forKey: "localImageFileUrl")
try data.write(to: localImageFileUrl!)
}

}
catch {
print("error")
}

关于ios - Swift mapview 无法显示缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60880416/

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