下载图像时UICollectionView崩溃

问题描述 投票:0回答:1

斯威夫特4:我收到了一个错误

'在打开一个Optional值时,意外地发现了nil'

当我从JSON查询填充downloadImage时,断点是我的UICollectionView

关于如何使这更安全的任何建议?或者我看错了什么?这是downloadImage代码:

extension UIImageView {
    @objc func downloadImage(from url: String){
        let urlRequest = URLRequest(url: URL(string: url)!)
        let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in

            if error != nil {
                print(error as Any)
                return
            }
            DispatchQueue.main.async {
                if UIImage(data: data!) != nil {
                self.image = UIImage(data: data!)
                }
            }

        }
        task.resume()

    }

这里是填充collectionView的函数:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "articleCell", for: indexPath) as! ArticleCell

        cell.title.text = self.articles?[indexPath.item].headline

//在这里崩溃

cell.imgView.downloadImage(from: (self.articles?[indexPath.item].imageURL!)!)

    cell.contentView.layer.cornerRadius = 0.5
    cell.contentView.layer.borderWidth = 1.0

    cell.contentView.layer.borderColor = UIColor.clear.cgColor
    cell.contentView.layer.masksToBounds = true

    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 0.5)
    cell.layer.shadowRadius = 1
    cell.layer.shadowOpacity = 0.5
    cell.layer.masksToBounds = false
    cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).cgPath


    return cell


}

这是控制台输出:

2017-12-18 06:55:08.412546+0100 Keyboard[81355:4788284] Failed to inherit CoreMedia permissions from 81260: (null)
2017-12-18 06:55:08.634498+0100 Keyboard[81355:4788188] [MC] Lazy loading NSBundle MobileCoreServices.framework
2017-12-18 06:55:08.635348+0100 Keyboard[81355:4788188] [MC] Loaded MobileCoreServices.framework
2017-12-18 06:55:08.637140+0100 Keyboard[81355:4788188] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/me/Library/Developer/CoreSimulator/Devices/9BAC6166-3559-4962-9715-F388A162E95F/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
Category: business
true
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2017-12-18 06:55:11.138236+0100 Keyboard[81355:4788188] Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) 

这是调试:

libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage(Swift.StaticString, Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never:
    0x113a5e420 <+0>:  pushq  %rbp
    0x113a5e421 <+1>:  movq   %rsp, %rbp
    0x113a5e424 <+4>:  pushq  %r15
    0x113a5e426 <+6>:  pushq  %r14
    0x113a5e428 <+8>:  pushq  %r13
    0x113a5e42a <+10>: pushq  %r12
    0x113a5e42c <+12>: pushq  %rbx
    0x113a5e42d <+13>: pushq  %rax
    0x113a5e42e <+14>: movl   %r9d, %r14d
    0x113a5e431 <+17>: movq   %r8, %r15
    0x113a5e434 <+20>: movq   %rcx, %rbx
    0x113a5e437 <+23>: movl   %edx, -0x2c(%rbp)
    0x113a5e43a <+26>: movq   %rsi, %r13
    0x113a5e43d <+29>: movq   %rdi, %r12
    0x113a5e440 <+32>: leaq   0x1c6551(%rip), %rdi      ; protocol descriptor for Swift._DefaultCustomPlaygroundQuickLookable + 136
    0x113a5e447 <+39>: movl   $0x28, %esi
    0x113a5e44c <+44>: movl   $0x7, %edx
    0x113a5e451 <+49>: callq  0x113a826b0               ; swift_rt_swift_allocObject
    0x113a5e456 <+54>: movq   %rbx, 0x10(%rax)
    0x113a5e45a <+58>: movq   %r15, 0x18(%rax)
    0x113a5e45e <+62>: movb   %r14b, 0x20(%rax)
    0x113a5e462 <+66>: movl   0x10(%rbp), %ecx
    0x113a5e465 <+69>: movl   %ecx, 0x24(%rax)
    0x113a5e468 <+72>: leaq   0xd4c81(%rip), %rcx       ; partial apply forwarder for closure #2 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._fatalErrorMessage(Swift.StaticString, Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never
    0x113a5e46f <+79>: movq   %r12, %rdi
    0x113a5e472 <+82>: movq   %r13, %rsi
    0x113a5e475 <+85>: movl   -0x2c(%rbp), %edx
    0x113a5e478 <+88>: movq   %rax, %r8
    0x113a5e47b <+91>: callq  0x1138aa1b0               ; function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer<A>((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A
->  0x113a5e480 <+96>: ud2    
    0x113a5e482 <+98>: nopw   %cs:(%rax,%rax)

这里是你们所询问的那个URL的控制台输出 - 它看起来像第一个类别加载图像确定,但是当点击下一个类别时它找到零。

Optional("http://www.afr.com/content/dam/images/h/0/6/g/p/6/image.related.socialLead.620x349.h06bfz.png/1513570709452.jpg")
Optional("http://www.afr.com/content/dam/images/h/0/6/b/z/i/image.related.afrArticleLead.620x365.h06aej.png/1513559758977.jpg")
Optional("http://www.afr.com/content/dam/images/h/0/6/f/x/o/image.related.afrArticleLead.620x365.h06bh0.png/1513563680637.jpg")
Optional("http://assets1.ignimgs.com/2017/12/15/15234538txuhe2cy-1513377967388_1280w.jpg")
Optional("http://assets1.ignimgs.com/2017/12/18/the-last-jedi-2-1513558653477_1280w.jpg")
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2017-12-18 07:07:03.222266+0100 Keyboard[87661:4872775] Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) 

我怎样才能在函数中优雅地接受这个零值?

ios arrays swift
1个回答
2
投票

尝试下面的代码,并始终尽量避免强制解包(!)。

extension UIImageView {

@objc func downloadImage(from url: String){

    if let urlconvert  = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),let urlConvertible = URL(string:urlconvert){

        let urlRequest = URLRequest(url: urlConvertible)
        let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in

            if error != nil {
                print(error as Any)
                return
            }
            DispatchQueue.main.async {
                if let data = data {

                    self.image = UIImage(data: data)
                }
            }

        }
        task.resume()

    }
  }

}

改变这个

cell.imgView.downloadImage(from: (self.articles?[indexPath.item].imageURL!)!)

至:

if let imageURL =  self.articles?[indexPath.item].imageURL{

cell.imgView.downloadImage(from: imageURL)
}
© www.soinside.com 2019 - 2024. All rights reserved.