我有一个问题,希望你能帮我解决。我在我的应用程序中使用了翠鸟,它很棒。在我的主用户搜索表视图中,它完美地加载和缓存图像。但奇怪的是,当我尝试在另一个表视图中缓存图像时,图像的宽度和高度都较大,它没有加载它们。奇怪的是,它通常只加载一个或两个图像。我认为这与图像的大小有关,但我并不是这样认为的。下面是一些代码,用于显示我正在下载图像并将其存储在firebase中
let storage = Storage.storage().reference().child(uid).child("userPhoto.jpg")
if let uploadData = UIImageJPEGRepresentation(photo, 0.2) {
storage.putData(uploadData, metadata: nil, completion:
{ (metadata, error) in
if error != nil {
print(error!)
return
}
if let imagerUrl = metadata?.downloadURL()?.absoluteString {
let ref = Database.database().reference()
let randomString = NSUUID().uuidString
print(randomString)
let postFeed = [randomString : imagerUrl]
print(self.photos)
self.collectionerView.reloadData()
self.activityer.stopAnimating()
ref.child("users").child(uid).child("profilePhotos").updateChildValues(postFeed)
}
})
}A现在了解我如何将urls加载/获取到数组中。imageView的高度和宽度= 150 * 150
func grabPhotos (uid: String) {
let ref = Database.database().reference()
ref.child("users").child(uid).child("profilePhotos").observeSingleEvent(of: .value, with: {(snapshot) in
if let theirPhotos = snapshot.value as? [String : AnyObject] {
for (_,one) in theirPhotos {
let newPhoto = Photo()
newPhoto.photoUrl = one as? String
print(one as! String)
self.imagers.append(newPhoto)
}
self.tablerView.reloadData()
} else {
self.activityer.stopAnimating()
}
}, withCancel: nil)
}最后是图像的缓存
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tablerView.dequeueReusableCell(withIdentifier: "userImagers", for: indexPath) as! UsersImagersTableViewCell
if let imagerl = self.imagers[indexPath.row].photoUrl {
let urler = URL(string: imagerl)
cell.imagerView.kf.setImage(with: urler)
} else {
cell.imagerView.image = UIImage(named: "profiler")
}
return cell
}所以总而言之,一些图片会加载,而另一些则不会,因为我会以同样的方式下载和存储它们。我想找到一个解决方案,所以如果你有一个,请回复。我真的很感激。
发布于 2017-12-17 15:46:25
尝试将它们缓存在文档中创建的文件夹中,如下所示
1-将此代码放入viewDidLoad
documentsUrl = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
var logsPath = documentsUrl.appendingPathComponent("Logs")
do {
try FileManager.default.createDirectory(at: logsPath!, withIntermediateDirectories: true, attributes: nil)
var res = URLResourceValues()
res.isExcludedFromBackup = true
try logsPath?.setResourceValues(res)
print("Created")
} catch let error as NSError {
NSLog("failed100 \(error.debugDescription)")
}2-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = areaSettTable.dequeueReusableCell(withIdentifier:CellIdentifier1) as! logTableViewCell
let tr = allTrips[indexPath.row]
let imgPath = documentsUrl.appendingPathComponent("\(tr.tripId).jpg")
print("qwqwqw \((imgPath?.path)!)")
if(FileManager.default.fileExists(atPath:(imgPath?.path)!))
{
do
{
if let data:Data? = try Data.init(contentsOf:imgPath!)
{
cell.mapImageV.image=UIImage.init(data:data!)
cell.imgActivity.isHidden = true
}
} catch let error as NSError {
NSLog("failed1150 \(error.debugDescription)")
}
}
else
{
if(tr.img != "nil" && tr.img != "")
{
cell.imgActivity.isHidden = false
cell.imgActivity.startAnimating()
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
let url = NSURL(string:tr.img) // myurl is the webpage address.
let data = NSData(contentsOf:url! as URL)
let imgIn = documentsUrl.appendingPathComponent("\(tr.tripId).jpg")
DispatchQueue.main.async
{
print("This is run on the main queue, after the previous code in outer block")
if !( data == nil )
{
data?.write(to:imgIn!, atomically: true)
self.areaSettTable.reloadData()
}
else
{
print("nillll data . . .")
}
}
}
}
else
{
cell.imgActivity.isHidden = true
}
}
return cell
}发布于 2017-12-17 16:49:51
这可能是我在编程中犯过的最愚蠢的错误之一。我不小心复制并粘贴了保存个人资料图像的代码来保存多个图像。为了存储这些图像并引用它们,它们需要不同的密钥。如果你看我上面的代码,它们不是单独的,这就是为什么它只下载一个图像的原因。通过做事情解决了它
let storage = Storage.storage().reference().child(uid).child("userPhotos").child("photo\(key)")浪费了3个小时,不管怎么笑
https://stackoverflow.com/questions/47853017
复制相似问题